Widget Fundamentals

The Tkinter module 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. You can also set or change options on an existing widget w by calling w .config( option = value ). You can get an option of w by calling 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, 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 of R, G, and B is a hexadecimal digit, to represent a color by the values ...

Get Python in a Nutshell 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.