-1
x = IntVar()
x.set(1)
def add():
    x.set(x.get()+1)

I am using this piece of code to update the question number on a tkinter quiz I am making.

self.recordNum = Label(self.quizf, text=x)
self.recordNum.pack()

self.Submit = Button(self.quizf, text = 'submit', command=lambda:[self.confirmAnswer(),add()])

I used lambda so I could have two commands in the same button which I need as self.confirmAnswer runs the if statement to check the answer and the add command activates the first piece of code displayed above.

When I run the quiz PY_VAR4 displays instead of a number, thanks for your help! :)

  • This is not a proper use of `lambda`. If you need to do more than one thing, create a proper function for the button. – Bryan Oakley May 02 '18 at 11:55

2 Answers2

1
self.recordNum = Label(self.quizf, text=x.get())
self.recordNum.pack()

self.Submit = Button(self.quizf, text = 'submit', command=lambda:[self.confirmAnswer(),add()])

In the label, you have to use x.get() instead of just x, as you want the value of x.

Just using x will reference to the object which is simply PY_VAR. The 4 at the end of the reference simply denotes the variable number to distinguish between other variables. In this case, the program in total so far has 5 variables declared, as references start from PY_VAR0 ... all the way to... PY_VAR4.

Using x.get() will instead get the value stored in the object from which the reference points to.

So instead of: self.recordNum = Label(self.quizf, text=x)

use: self.recordNum = Label(self.quizf, text=x.get())

Possible duplicate of: Tkinter IntVar returning PY_VAR0 instead of value

RealPawPaw
  • 988
  • 5
  • 9
0

You need to use “textvariable” as argument for your variable. Then the label will update itself.

deets
  • 6,285
  • 29
  • 28