Buttons are standard widgets in a GUI. They come with the default Tkinter module and you can place them in your window.
A Python function or method can be associated with a button. This function or method is named the callback function. If you click the button, the callback function is called.
A note on buttons: a tkinter button can only show text in a single font. The button text can be multi line. That means that this widget won't show icons next to the text, for that you'd need another widget.
Practice now: Test your Python skills with interactive challenges
Example
Introduction
You can create and position a button with these lines:
exitButton = Button(self, text="Exit", command=self.clickExitButton)
exitButton.place(x=0, y=0)
The callback method is clickExitButton, which is assigned in the above line (command=). This is a simple method:
def clickExitButton(self):
exit()
Without a callback method, a button is shown but clicking it won't do anything.
This window should show up:
Button example
To run the example, save it as button.py and run it with the python interpreter. This example opens a window, shows a button and you can click the button.
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
# widget can take all window
self.pack(fill=BOTH, expand=1)
# create button, link it to clickExitButton()
exitButton = Button(self, text="Exit", command=self.clickExitButton)
# place button at (0,0)
exitButton.place(x=0, y=0)
def clickExitButton(self):
exit()
root = Tk()
app = Window(root)
root.wm_title("Tkinter button")
root.geometry("320x200")
root.mainloop()