How it works...

To connect a Boolean variable to the Checkbutton entry, we first define BooleanVar and then create the entry by calling add_checkbutton using the variable option.

Remember that the onvalue and offvalue options should match the type of the Tkinter variable, as we do with regular RadioButton and CheckButton widgets:

self.checked = tk.BooleanVar()
self.checked.trace("w", self.mark_checked)
# ...
submenu.add_checkbutton(label="Checkbutton", onvalue=True,
offvalue=False, variable=self.checked)

Radiobutton entries are created in a similar fashion using the add_radiobutton method, and only a single value option to set to the Tkinter variable when the radio is clicked. Since StringVar initially holds the empty string value, we set it to the first radio value so that it will display as checked:

self.radio = tk.StringVar()
self.radio.set("1")
self.radio.trace("w", self.mark_radio)
# ...
submenu.add_radiobutton(label="Radio 1", value="1",
variable=self.radio)
submenu.add_radiobutton(label="Radio 2", value="2",
variable=self.radio)
submenu.add_radiobutton(label="Radio 3", value="3",
variable=self.radio)

Both variables trace the changes with the mark_checked and mark_radio methods, which simply print the variable values into the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset