The Button widget

The ttk.Button widget should also be familiar from Chapter 1, Introduction to Tkinter. It's just a straightforward button that you click with the mouse or spacebar, as shown in the following screenshot:

Just like the Checkbutton widget, this widget uses the text and textvariable configuration options to control the label on the button. The Button objects don't take variable, but they do take a command argument, which specifies a Python function to run when the button is clicked.

The following example shows the use of a Button object:

tvar = tk.StringVar()
def swaptext():    if tvar.get() == 'Hi':        tvar.set('There')    else: tvar.set('Hi') ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.