Goal
I am creating a menu inside an application. In that I want a radiobutton. And by default I want the radiobutton, to be in the on
state.
Research
I found how to add the radiobutton using the options.add_radiobutton()
command here
TKinter effbot . But I still don't know which of the options needs to be used so that in the default it is set to on.
Code
optionsmenu = Menu(menubar,tearoff=0)
optionsmenu.add_radiobutton(label='Pop Up set to on??',command=self.togglePopUp)
code for self.togglePopUp
:
def togglePopUp(self,event=None):
if self.showPopUp:
self.showPopUp = False
else:
self.showPopUp = True
I will initialise self.showPopUp
as True
.
Please Help me with setting the radiobutton to the on
position in the default mode.
IntVar(). get() returns the onvalue or ofvalue of the Checkbutton, based on the status. You can either assign a function to command option of Checkbutton, and read the status of Checkbutton as and when the button is checked or not.
The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user can then select one or more options by clicking the button corresponding to each option. You can also display images in place of text.
_ClassType IntVarConstruct an integer variable. set(self, value) Set the variable to value, converting booleans to integers. get(self) Return the value of the variable as an integer.
Once the checkbox is selected, we are calling prop() function as prop( "checked", true ) to check the checkbox and prop( "checked", false ) to uncheck the checkbox.
If you want to toggle boolean values, I suggest you to use add_checkbutton()
instead of add_radiobutton()
.
With the radiobutton you only have a static value
option, which does not change when the entry is clicked. On the other hand, checkbuttons allow you to change between the onvalue
and offvalue
options.
self.var = IntVar(root)
self.var.set(1)
optionsmenu.add_checkbutton(label='Pop Up set to on??', command=self.togglePopUp,
variable=self.var, onvalue=1, offvalue=0)
Note that the IntVar you have to use as variable for the meny entry can replace the self.togglePopUp
variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With