Skip to content Skip to sidebar Skip to footer

Tkinter Checkbox Variable Is Always 0

I'm using Python's TkInter module for a GUI. Below is a simple checkbox code. def getCheckVal(): print cbVar.get() windowTime=Tk.Tk() cbVar = Tk.IntVar() btnC = Tk.Checkbutton

Solution 1:

Your windowTime, cbVar, etc. variables are defined in the function's local scope. When askUser() completes execution, those values are thrown away. Prepend self. to them to save them as instance variables.

There should only be one mainloop() in your program, to run the main Tkinter root object. Try putting it as the very last line in the program. I recommend doing some reading on Effbot for how to set up a Tkinter application.

Solution 2:

I'm not sure what all you're trying to do, but one problem is that the TK.IntVar called cbVar that you create in your askUser() method will be deleted when the function returns, so you need to attach it to something that will still exist after that happens. While you could make it a global variable, a better choice would be to make it an attribute of something more persistent and has a longer "lifespan".

Another likely issue is that generally there should only be one call to mainloop() in a single Tkinter application. It appears what you want to do is display what is commonly known as a Dialog Window, which Tkinter also supports. There's some standard ones built-in, plus some more generic classes to simplify creating custom ones. Here's some documentation I found which describes them in some detail. You may also find it helpful to look at their source code. In Python 2 it's in the /Lib/lib-tk/tkSimpleDialog.py file and in Python 3 the code's in a file named /Lib/tkinter/simpledialog.py.

Below is code that takes the latter approach and derives a custom dialog class named GUIButtonDialog from the generic one included the Tkinter library which is simply named Dialog.

try:
    import Tkinter as Tk    # Python 2from tkSimpleDialog import Dialog
except ModuleNotFoundError:
    import tkinter as Tk    # Python 3from tkinter.simpledialog import Dialog


classGUIButtonDialog(Dialog):
    """Custom one Button dialog box."""def__init__(self, btnText, parent=None, title=None):
        self.btnText = btnText
        Dialog.__init__(self, parent, title)

    defgetCheckVal(self):
        print(self.cbVar.get())

    defbody(self, master):
        """Create dialog body."""
        self.cbVar = Tk.IntVar()
        self.btnC = Tk.Checkbutton(master, text=self.btnText, variable=self.cbVar,
                                   command=self.getCheckVal)
        self.btnC.grid()

        return self.btnC  # Return the widget to get inital focus.defbuttonbox(self):
        # Overridden to suppress default "OK" and "Cancel" buttons.passclassGUIMainClass:

    def__init__(self):
        """Create the main window."""
        self.window = Tk.Tk()

    defaskUser(self):
        """Display custom dialog window (until user closes it)."""
        GUIButtonDialog("Save", parent=self.window)

    defcmdWindow(self):
        frameShow = Tk.Frame(self.window)
        frameShow.grid()
        btnSwitch = Tk.Button(frameShow, text='Show Plots', command=self.askUser)
        btnSwitch.grid()
        self.window.mainloop()


GUIObj = GUIMainClass()
GUIObj.cmdWindow()

Post a Comment for "Tkinter Checkbox Variable Is Always 0"