Tkinter: Use Root.after() Properly
I want to create a projectile that every second move 10 pixel along his y axis using the after() function. The first attempt I've done is this: def muovi(self, root): i = 0
Solution 1:
This code:
root.after(1000, self.muovi(root))
is functionally identical to this code:
result= self.muovi(root)
root.after(1000, result)
Do you see the problem? You are callingself.muovi
and giving the result to after
. Instead, you must supply a reference to self.muovi
. Additional positional arguments can be used as arguments following the reference:
root.after(1000, self.muovi, root)
Solution 2:
Your first piece of code does not work because you haven't specified the function that will be called after 1000 ms.
And your second piece of code does not work because the second argument expected by after
is a function, like for the command
option of a button.
Here is an example:
from tkinter import Tk, Canvas, Button
def move():
canvas.move(circle, 0, 10)
root.after(1000, move) # the second argument as to be a function, not move()
root = Tk()
canvas = Canvas(root)
canvas.pack(fill='both', expand=True)
circle = canvas.create_oval(10,10,30,30, fill='red')
Button(root, text="Start", command=move).pack()
root.mainloop()
Post a Comment for "Tkinter: Use Root.after() Properly"