I am attempting to pass a variable to a function that updates a screen widget. The following code works:
import tkinter as tk
import random``
def update():
l.config(text=str(random.random()))
root.after(1000, update)
root = tk.Tk()
l = tk.Label(text='0')
l.pack()
update()
root.mainloop()
However if I try to pass a variable it doesn't work.
import tkinter as tk
import random
def update(a):
l.config(text=str(random.random() + a))
root.after(1000, update)
root = tk.Tk()
l = tk.Label(text='0')
l.pack()
a=1
update(a)
root.mainloop()
and the following error appears on the screen:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1541, in __call__
return self.func(*args)
File "/usr/lib/python3.4/tkinter/__init__.py", line 590, in callit
func(*args)
TypeError: update() missing 1 required positional argument: 'a'
Is there a way around this? Need to pass the variable.
Thanks in advance.