liveBook · Manning

Button

As explained in Get Programming: Learn to code with Python

A button is a graphical user-interface widget that can be activated to initiate an action.

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

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains creation, placement, command binding, and click results, while deferring broader widget and event-loop details.
Examples
Demonstrates colored buttons, a background-changing button, a countdown control, and Add and Show buttons for a phonebook.
What it assumes
Builds from text-based programs toward windows, widgets, and event handlers without assuming an existing GUI vocabulary.
What you can do after
Apply it via tkinter to build clickable controls that invoke functions and coordinate other GUI widgets.
How it connects
Connects buttons to tkinter, windows, widgets, functions, event handlers, text boxes, labels, and the main loop.

Also explained in

Compare how 8 books explain button →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.