1

Curious as to the capabilities/intended use of this attribute as mentioned here ( 'class=' )

user2497792
  • 505
  • 2
  • 9
  • 18

2 Answers2

4

Bindings

You can associate bindings with the widget class. Thus, you could have N entry widgets and assign them the same class, and then give them all the same bindings without having to duplicate the bindings on every widget.

It's also useful if you want to remove all of the default bindings, since the default bindings belong to the default classes. If you change the class, they no longer will have their default behavior.

More specifically, the class of a widget is automatically added to the list of bindtags for that widget, and it is the list of bindtags that give a widget its behavior. For a very brief overview of bindtags, you can see my answer to the question "Basic query regarding bindtags in tkinter"

Widget Attributes

In Tkinter, you can define default attributes of widgets with the option_add command (there are also commands such as option_get and option_clear). This command uses an "option database" borrowed from the world of X11 (the unix/linux windowing system). You can apply options to classes. So, for example, you can define your own class of frame and give it a unique combination of borderwidth, relief and color. When you create new frames, they will inherit these attributes.

This was quite useful when tk was used primarily with Tcl, and primarily on unix systems, since tcl didn't have a true object-oriented way of subclassing widgets. With Tkinter it's a little less useful since you can subclass widgets and simply change the attributes in the constructor.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
3

It's described here:

Specifies the window class. The class is used when querying the option database for the window's other options, to determine the default bindtags for the window, and to select the widget's default layout and style. This is a read-only option: it may only be specified when the window is created, and may not be changed with the configure widget command.

Or more detailled in this tutorial (section 1.2).

Note that in python, as class is a keyword, you can't write:

frame = tk.Frame(root, class='Spam')

you'll need to use:

frame = tk.Frame(root, cnf={'class': 'Spam'})  # or
frame = tk.Frame(root, **{'class': 'Spam'})
mata
  • 67,110
  • 10
  • 163
  • 162