liveBook · Manning

Widget

As explained in Get Programming: Learn to code with Python

A widget is a graphical user-interface component that displays information or accepts user interaction within a window.

A widget is a graphical element inside a GUI window that displays information or lets users interact with a program. Buttons, menus, text boxes, labels, scrollbars, and canvases are widgets. section 37.1

Build a widget into a GUI in three steps:

  • Create the widget, such as a Button, Entry, or Label. section 37.3
  • Place it in the window with a layout method such as pack. section 37.3
  • Connect an event handler when the widget should respond to an action such as a click or menu selection. section 37.1 section 37.4

A button that changes the window

import tkinter def change_color(): window.configure(background="white") window = tkinter.Tk() white = tkinter.Button(window, text="Click", command=change_color) white.pack() window.mainloop() The button is created with a parent window, given a label, and connected to change_color. Clicking it runs the function and changes the window background. section 37.4

Table 37.1. Widgets available in tkinter

Widget name

Description

Widget name

Description

Button Shows a button Menubutton Shows menus
Canvas Draws shapes OptionMenu Shows a pop-up menu
Checkbutton Shows options via checkboxes (can select more than one option) PanedWindow Contains window panes that can be resized
Entry A text field the user can type text in Radiobutton Shows options via radio buttons (can select only one option)
Frame Container to put other widgets in Scale Shows a slider
Label Shows single line of text or an image Scrollbar Adds scrollbars to other widgets
LabelFrame Container to add space Spinbox Like an Entry, but can select only from some text
Listbox Shows options via a list Text Shows multiple lines of text
Menu Shows commands (contained inside Menubutton) TopLevel Allows for a separate window container

Browse the tkinter widget table to compare controls such as Button, Entry, Label, Checkbutton, Radiobutton, Canvas, and Scrollbar. section 37.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains creation, placement, types, and event handling, then applies them in a countdown example.
Examples
Uses buttons, text boxes, labels, progress bars, menus, and a countdown timer.
What you can do after
Build it from scratch by adding tkinter widgets, placing them in a window, and connecting actions to event handlers.
How it connects
Connects widgets with GUI windows, tkinter, layout methods, functions, and event handlers.

Also explained in

Compare how 2 books explain widget →

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.