A button is a graphical user-interface widget that a user can activate to start an action. In a Python GUI, tkinter creates the button and places it inside a window. section 37.3 section 37.1
Build a button in two steps, then give it behavior:
- Create it with a parent window, optional display text, and settings such as background color. section 37.3
- Place it in the window with pack(). section 37.3
- Connect it to a function with the command option. That function runs when the user clicks the button. section 37.4
A button that changes the window
import tkinter def change_color(): window.configure(background="white") window = tkinter.Tk() window.configure(background="grey") white = tkinter.Button(window, text="Click", command=change_color) white.pack() window.mainloop() The function changes the window color. The button names that function as its command; it does not call the function while the button is being created. The window waits for a click inside mainloop(), then runs the handler.
Practice connection
Buttons can coordinate several widgets. A countdown program uses one button to read a number from a text box, update a label, and count down to zero. A phonebook can use an Add button to store contact fields and a Show button to display them. section 37.4 section 37.5







