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.
Related course: Python Desktop Apps with Tkinter
Example
Introduction
You can create and position a button with these lines:
1
2
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:
1
2
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.
1 |
from tkinter import * |