Widget Fundamentals

Tkinter supplies many kinds of widgets, and most of them have several things in common. All widgets are instances of classes that inherit from class Widget. Class Widget itself is abstract; that is, you never instantiate Widget itself. You only instantiate concrete subclasses corresponding to specific kinds of widgets. Class Widget’s functionality is common to all the widgets you instantiate.

To instantiate any kind of widget, call the widget’s class. The first argument is the parent window of the widget, also known as the widget’s master. If you omit this positional argument, the widget’s master is the application’s main window. All other arguments are in named form: option=value. To set or change options on an existing widget w, call w.config(option=value). To get an option of w call w.cget('option'), which returns the option’s value. Each widget w is a mapping, so you can also get an option as w['option'], and set or change it with w['option']=value.

Common Widget Options

Many widgets accept some common options. Some options affect a widget’s colors, while others affect lengths (normally in pixels), and there are various other kinds. This section details the most commonly used options.

Color options

Tkinter represents colors with strings. The string can be a color name, such as 'red' or 'orange', or it may be of the form '#RRGGBB', where each R, G, and B are hexadecimal digits that represent a color by the values of red, green, and blue components on a scale of ...

Get Python in a Nutshell, 2nd Edition 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.