python - item.config in Tkinter execute together -
i coding in python2.7 using tkinter. need set of rectangles change color 1 after another. when write item.config 1 after 1 in function, execute @ end of execution.
def button(self): print "hi" self.canvas.itemconfig(self.figure1, fill="green") time.sleep(3) print "hello" self.canvas.itemconfig(self.figure2, fill="green") time.sleep(3) print "okay" time.sleep(3) self.canvas.itemconfig(self.figure3, fill="green")
when run this, print statements complete , 3 figures change color together. need change 1 after other each statement gets executed/printed.
i new python , tkinter. suggestions , ideas why happening? thanks!
here simple example demonstrates how perform chain of actions using after
delay events while keeping tk event loop running. have python 3 may need modify work python 2.7.
#!/usr/bin/python tkinter import * def toggle(canvas, items): if len(items) > 0: canvas.itemconfigure(items[0], fill="green") canvas.after(1000, lambda: toggle(canvas, items[1:])) return root = tk() canvas = canvas(root, background="white") canvas.pack(fill='both', expand=true) figure1 = canvas.create_rectangle(10,10,80,80, fill="red") figure2 = canvas.create_rectangle(100,10,180,80, fill="red") figure3 = canvas.create_rectangle(200,10,280,80, fill="red") canvas.after(1000, lambda: toggle(canvas, [figure1, figure2, figure3])) root.mainloop()
keeping event loop operating crucial. cannot use sleep
in gui applications unless create worker thread. typically don't need anyway. after
call puts task onto ui event queue run @ later time , if capture after token can cancel them (for instance if require timeout function something.).
Comments
Post a Comment