Sam Carpenter
Sam Carpenter

Reputation: 427

Python tkinter IntVars not being set properly

I'm working on a tkinter program in Python that uses four radiobuttons and one checkbutton. The project takes advantage of both the GUI window and the command-line window. The code is supposed to create the buttons, display a command in the box, wait for a few seconds, then do something based on what buttons have or haven't been pressed.

Here is the code for my creation of the buttons -

def __button1__(self):

    radiovar = tkinter.IntVar()

    check1var = tkinter.IntVar()

    radiovar.set(0)

    check1var.set(0)

    self.frame1 = tkinter.Frame(self.main_window)

    radio1 = tkinter.Radiobutton(self.frame1, text = 'Fire', variable = radiovar, value = 1)

    radio2 = tkinter.Radiobutton(self.frame1, text = 'Earth', variable = radiovar, value = 2)

    radio3 = tkinter.Radiobutton(self.frame1, text = 'Air', variable = radiovar, value = 3)

    radio4 = tkinter.Radiobutton(self.frame1, text = 'Water', variable = radiovar, value = 4)

    check1 = tkinter.Checkbutton(self.frame1, text = 'Shadow', variable = check1var,)

    radio1.pack()

    radio2.pack()

    radio3.pack()

    check1.pack()

    radio4.pack()

    self.frame1.pack(side = 'left')

    self.frame1.after(100, self.__game__)

This is part of a class that creates the GUI. The final line waits for one second, then launches the function that does the rest of the program. The function is -

def __game__(self):

    print('Select Shadow, then click on Water.')
    self.frame1.after(500)
    if check1var.get() == 1 and radiovar.get() == 4:
        print('Nicely done!')

The window is created, the second passes, then I get an exception saying that check1var (the IntVar that goes along with the check box) is undefined. I see that I'm doing something wrong with either setting or checking the IntVar - what am I doing wrong, and how do I fix this?

Upvotes: 0

Views: 197

Answers (2)

mmi
mmi

Reputation: 491

to use one variable inside multiple functions of a class, you have to declare them as attribute of the class, which means defining them with self.varname = var and get them like self.varname:

def __button1__(self):

    self.radiovar = tkinter.IntVar()

    self.check1var = tkinter.IntVar()

    self.radiovar.set(0)

    self.check1var.set(0)

    self.frame1 = tkinter.Frame(self.main_window)

    radio1 = tkinter.Radiobutton(self.frame1, text = 'Fire', variable = self.radiovar, value = 1)

    radio2 = tkinter.Radiobutton(self.frame1, text = 'Earth', variable = self.radiovar, value = 2)

    radio3 = tkinter.Radiobutton(self.frame1, text = 'Air', variable = self.radiovar, value = 3)

    radio4 = tkinter.Radiobutton(self.frame1, text = 'Water', variable = self.radiovar, value = 4)

    check1 = tkinter.Checkbutton(self.frame1, text = 'Shadow', variable = check1var,)

    radio1.pack()

    radio2.pack()

    radio3.pack()

    check1.pack()

    radio4.pack()

    self.frame1.pack(side = 'left')

    self.frame1.after(100, self.__game__) 

and:

def __game__(self):

    print('Select Shadow, then click on Water.')
    self.frame1.after(500)
    if self.check1var.get() == 1 and self.radiovar.get() == 4:
        print('Nicely done!')

Upvotes: 1

tobias_k
tobias_k

Reputation: 82889

Assuming that those methods are part of the same class, use self to make them attributes of the instance, so they can be accessed in all methods.

def __button1__(self):
    self.radiovar = tkinter.IntVar()
    self.check1var = tkinter.IntVar()
    self.radiovar.set(0)
    self.check1var.set(0)
    # ... define buttons using self.checkvar and self.radiovar

def __game__(self):
    print('Select Shadow, then click on Water.')
    self.frame1.after(500)
    if self.check1var.get() == 1 and self.radiovar.get() == 4:
        print('Nicely done!')

Upvotes: 1

Related Questions