tkinter button

Add buttons to your window


The Button component can contain text or images. You can associate a Python function or method with it, and when the button is pressed, the corresponding function or method will be is automatically executed.

The Button component can only display text in a single font, but text can span multiple lines. It is also possible to underline individual characters (e.g. to indicate keyboard shortcuts). By default, the tab key is used to switch between buttons.

There are different types of buttons like Buttons, Checkbutton and Radiobutton. The later two components are better suited for data entry buttons. For more examples follow this link.

Related course: Python Desktop Apps with Tkinter

python tkinter button

Normal buttons are very easy to use. All you need to do is specify the contents of the Button (text, bitmap or image) and associate the function or method that should be called when.

import tkinter as tk

master = tk.Tk()

def callback():
    print("I've been called!")

b = tk.Button(master, text="Execute", command= callback)
b.pack()

master.mainloop()

tkinter button in python

tkinter button disabled

If a button does not have a function or method associated with it, then it is useless. You may use such buttons in the course of developing your application, in which case it would be better to disable them. so as not to cause confusion with your test users.

b = tk.Button(master, text="do not execute", state=" disabled")

tkinter button disabled

tkinter button fill

If you don't specify the size of the Label, then the Label is just the right size to fit its contents. You can use the padx and pady options to add extra spacing between the content of the Button and the border. The Button size can be set explicitly through the height and width options.

Of course you can set the size of the Button explicitly through the height and width options: if you're displaying text, they define the Button size in text units; if you're displaying a bitmap or an image, they define the Button size in pixels (or other on-screen units).

For Button components with text, you can specify the size of the Buttton in pixels , but this requires some skill. Here's one way to do it (there are other ways to implement it).

f = tk.Frame(master, height=64, width=64)
f.pack_propagate(0)
f.pack()

b = tk.Button(f, text="OK", command= callback)
b.pack(fill="both", expand=1)

tkinter button fill

tkinter button text

Button can display multiple lines of text, you can use line feeds directly or use the wraplength option. to do so. You can use the anchor and justify and padx options to Make the text appear as you want it to.

longtext = """
I'm obviously just a button.
It doesn't take much to be a button.
The text is used to tell the user when
It happens when I get pushed.
What's the matter, but why me?
That long?
"""

b = tk.Button(master, text=longtext, anchor ="w", justify="left", padx=2, command= callback)
b.pack()

python tkinter multiline button

In order to keep an ordinary button "pressed", you can simply change the default value of the relief option from "raised" to "sunken":.

b.config(relief="raised")

You may also want to change the background color. But a better way is to use the Checkbutton and Radiobutton components, and put the Their indicatoron option is set to False.

b = tk.Checkbutton(master, image=bold, variable=var, indicatoron=False)

python tkinter button image

In earlier versions of Tkinter, the image option overrides the text option. This meant that if you specified both options, only the image option would be shown. But in the newer Tkinter, you can use the compound option to set a mixed mode between the two. For example, here is how you can make the text sit on top of the image by setting compound="center" (overlapping) (shown).

photo = tk.PhotoImage(file = 'botton.gif')
b = tk.Button(master, text="click me", font = 20, image = photo, compound = "center")
b.pack()

python tkinter image button

So you would have this program:

import tkinter as tk

master = tk.Tk()

def callback():
    print("I've been called!")

photo = tk.PhotoImage(file = 'button.gif')
b = tk.Button(master, text="", font = 20, image = photo, compound = "center")                                                                       
b.pack()

master.mainloop()