Global Variable Acting as a Local Variable in Python Tkinter
Question:
I am trying to create a stopwatch in tkinter and I need a counter
variable in order to be able to do so. However, the issue is that the variable is acting as a local one even though I declared it as global.
Here is my script:
import tkinter as tk
root = tk.Tk()
global counter
counter = 0
def go():
label.config(text=str(counter))
counter+=1
root.after(1000,go2)
def go2():
label.config(text=str(counter))
counter+=1
root.after(1000,go)
def stop():
label.config(text=str(0))
gobutt = tk.Button(text = "Go", command = lambda: go())
stopbutt = tk.Button(text = "Stop", command = lambda: go2())
gobutt.pack()
stopbutt.pack()
label = tk.Label(text = "0")
label.pack()
root.mainloop()
Here is my error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
File "/Users/MinecraftMaster/Desktop/Python/Tests/TkinterTest/Tkinter Test.py", line 29, in <lambda>
gobutt = tk.Button(text = "Go", command = lambda: go())
File "/Users/MinecraftMaster/Desktop/Python/Tests/TkinterTest/Tkinter Test.py", line 18, in go
label.config(text=str(counter))
UnboundLocalError: local variable 'counter' referenced before assignment
Answers:
You should put global counter
inside your function definitions so that the counter
referenced inside your function is taken the one defined within the global scope
counter = 0
def go():
global counter
label.config(text=str(counter))
counter+=1
root.after(1000,go2)
def go2():
global counter
label.config(text=str(counter))
counter+=1
root.after(1000,go)
I am trying to create a stopwatch in tkinter and I need a counter
variable in order to be able to do so. However, the issue is that the variable is acting as a local one even though I declared it as global.
Here is my script:
import tkinter as tk
root = tk.Tk()
global counter
counter = 0
def go():
label.config(text=str(counter))
counter+=1
root.after(1000,go2)
def go2():
label.config(text=str(counter))
counter+=1
root.after(1000,go)
def stop():
label.config(text=str(0))
gobutt = tk.Button(text = "Go", command = lambda: go())
stopbutt = tk.Button(text = "Stop", command = lambda: go2())
gobutt.pack()
stopbutt.pack()
label = tk.Label(text = "0")
label.pack()
root.mainloop()
Here is my error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
File "/Users/MinecraftMaster/Desktop/Python/Tests/TkinterTest/Tkinter Test.py", line 29, in <lambda>
gobutt = tk.Button(text = "Go", command = lambda: go())
File "/Users/MinecraftMaster/Desktop/Python/Tests/TkinterTest/Tkinter Test.py", line 18, in go
label.config(text=str(counter))
UnboundLocalError: local variable 'counter' referenced before assignment
You should put global counter
inside your function definitions so that the counter
referenced inside your function is taken the one defined within the global scope
counter = 0
def go():
global counter
label.config(text=str(counter))
counter+=1
root.after(1000,go2)
def go2():
global counter
label.config(text=str(counter))
counter+=1
root.after(1000,go)