Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default value of a checkbutton in a menu to True

Tags:

python

tkinter

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.

like image 404
IcyFlame Avatar asked Mar 23 '13 09:03

IcyFlame


People also ask

How do I find the value of my Checkbutton?

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.

What is Checkbutton in Python?

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.

What is IntVar () in tkinter?

_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.

How do you uncheck a checkbox in Python?

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.


1 Answers

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.

like image 126
A. Rodas Avatar answered Nov 15 '22 01:11

A. Rodas