Setting the default value of the control button in the menu to True

goal

I create a menu inside the application. In this I want a beacon. And by default, I want the radio object to be on .

Study

I found how to add a radio cassette using the options.add_radiobutton() command here Tcinter effbot . But I still don’t know which parameter to use so that it is enabled by default.

code

 optionsmenu = Menu(menubar,tearoff=0) optionsmenu.add_radiobutton(label='Pop Up set to on??',command=self.togglePopUp) 

for self.togglePopUp :

 def togglePopUp(self,event=None): if self.showPopUp: self.showPopUp = False else: self.showPopUp = True 

I initialize self.showPopUp as True .

Please help me set the cassette to on in the default mode.

+4
source share
2 answers

If you want to switch boolean values, I suggest you use add_checkbutton() instead of add_radiobutton() .

Using radio, you only have the static parameter value , which does not change when you click on a record. On the other hand, control buttons allow you to change the onvalue and offvalue .

 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) 

Please note that IntVar, which you should use as a variable for standard recording, can replace the variable self.togglePopUp .

+4
source

As @A Rodas mentioned:

 self.var = IntVar() self.var.set(1) optionsmenu.add_checkbutton(label='Pop Up set to on??', command=self.togglePopUp, variable=self.var, onvalue=1, offvalue=0) 

And to return the value of this variable, use:

 if self.var.get() == 1: self.showpopup() else: print 'popup has been disabled. you can toggle this option in the options menu' 
0
source

All Articles