Shimon Pokrivka
Shimon Pokrivka

Reputation: 55

Python Tkinter - How to set the default value of a radiobutton?

I've been searching for this a while, but no answers seemed to help me. So. I have this one RadioButton (actually three but don't worry about it) and I want to set the default value of it (the value which represents the unchecked box).

I've tried the "value" option but python doesn't recognize it. Here's what I have:

RadioButton1 = ttk.Checkbutton(frame2radio, text="Bold", width=20, cursor="hand2", underline=True, value=0)

Upvotes: 1

Views: 9483

Answers (1)

GetHacked
GetHacked

Reputation: 554

ttk.Checkbutton documentation from NMT

"To change the state of a checkbutton through program control, use the .set() method of the associated control variable."

When you create your checkButton, add variable=myVar to the arguments. Then, when you want to select it, you can call myVar.set(1).

Example:

RadioButton1 = ttk.Checkbutton(frame2radio, text="Bold", width=20, cursor="hand2", underline=True, value=0, variable=myVar)
myVar.set(1)

The button will represent this change on the next main window loop or when re-drawn.


If you wanted to do this in a Combobox, as mentioned in your comment, you can select a value from the combobox using the combobox's .set(value) function. Note that value has to be one of the options available in your Combobox.

Example:

myComboBox.set("Option 1")

Upvotes: 3

Related Questions