today I am studying tkinter and meet something strange, can anyone help?
Here is my first code:
from tkinter import *
def click_me():
count += 1
print(f'You press {count} times button!')
root = Tk()
count = 0
Button(root, text='click me', command=click_me).pack()
root.mainloop()
I run this code get info:
local variable 'count' referenced before assignment
I can understand it because when I learn python, they told me I need use global
or click_me(count)
to do this job.
Here is my second code:
from tkinter import *
def select():
print(f'Checkbutton value is {str(var.get())}')
root = Tk()
var = IntVar() # return bool value into 1 or 0.
Checkbutton(root, text='click me', variable=var, command=select).pack()
root.mainloop()
I can run second code without any error. So I think count
and var
both are variable, the differece are one is regular variable and another are tkinker varialbe, is that reason?