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:

tkinter button

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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()

Download Tkinter Example