[Tkinter] tkinter: after issue - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] tkinter: after issue (/thread-29269.html) |
tkinter: after issue - edwin6938 - Aug-25-2020 Hi i have a problem with a frame gathering an image that i update every 2s. It through me the error: invalid command name "3186233555656<lambda>" while executing "3186233555656<lambda>" ("after" script)My code is the following: class MainCanvas(tk.Frame): def __init__(self,container,directory,...): ... self.directory=directory self.container=container self.canvas = tk.Canvas(self.container) img=Image.open(self.directory) self.photo=ImageTk.PhotoImage(img.resize(self.resolution, Image.ANTIALIAS)) self.canvas.image=self.photo self.imageCreated = self.canvas.create_image(0, 0, anchor=NW, image=self.photo) self.canvas.grid(...) def change_image(self,list_directory,index): directory=list_directory[index] img=Image.open(directory) if index==2: nextindex=0 else: nextindex=index+1 self.photo=ImageTk.PhotoImage(img.resize(self.resolution, Image.ANTIALIAS)) self.canvas.itemconfig(self.imageCreated,image=self.photo) self.canvas.after(2000, lambda:self.change_image(list_directory, nextindex ))For me maybe the problem could come from the "loop", but i don't know how to solve it. Thanks for your help RE: tkinter: after issue - Larz60+ - Aug-25-2020 you could write your timer as a method, and have it call your change image upon completion like example below example replace None with your method: import tkinter as tk from datetime import datetime class TkSimpleTimer: def __init__(self, parent): self.parent = parent def timeit(self, t_time): start_time = datetime.now().strftime("%H:%M:%S:%f") print(f"\nTimer started at: {start_time}, will delay {t_time} ms") self.parent.after(t_time, None) end_time = datetime.now().strftime("%H:%M:%S:%f") print(f"Timer finished at: {end_time}") if __name__ == '__main__': root = tk.Tk() tst = TkSimpleTimer(root) for i in range(5): # time is in miliseconds tst.timeit(i*1000) root.destroy() root.mainloop()
|