In meinem sehr simplen Beispielprogramm bewegt sich nach 1 Sekunde ein rotes Rechteck.
Code: Alles auswählen
import time
from tkinter import *
if __name__ == "__main__":
root = Tk()
root.attributes("-fullscreen", True)
root.focus_set()
root.bind("<Escape>", lambda e: root.quit())
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
center_x = width / 2
center_y = height / 2
window = Canvas(root, width=width, height=height, bg="black")
window.pack()
a = window.create_rectangle(50, 0, 150, 50, fill='red')
window.update()
time.sleep(1)
window.move(a, 100, 100)
root.mainloop()
Code: Alles auswählen
from tkinter import *
if __name__ == "__main__":
root = Tk()
root.attributes("-fullscreen", True)
root.focus_set()
root.bind("<Escape>", lambda e: root.quit())
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
center_x = width / 2
center_y = height / 2
window = Canvas(root, width=width, height=height, bg="black")
window.pack()
a = window.create_rectangle(50, 0, 150, 50, fill='red')
window.update()
root.after(1000, window.move(a, 100, 100))
root.mainloop()
- Warum ist `root.after()` besser als `time.sleep()`?
- Warum funktioniert die Verschiebung des Rechtecks nur, wenn `window.update()` vor der Anweisung zum Verschieben des Rechtecks `window.move()` kommt?
Atalanttore