May-30-2020, 06:32 AM
I've been playing around with an earlier post. I added a reset timer to the widget. The first run through the timer works as expected but, the next run through the timer runs fast and even skips numbers at times. I can get it to work correct but then I get errors in the console. Any advice would be great. I'm trying to use the after_cancel and after_idle methods.
#! /usr/bin/env python3 from tkinter import * from tkinter import ttk class Picture: def __init__(self, parent): self.parent = parent img = PhotoImage(file='images/img1.png') style = ttk.Style() style.configure('My.TLabel',padding=.25, borderwidth=5, relief='groove') self.label = ttk.Label(self.parent, style='My.TLabel') self.label['image'] = img img.image = img self.label.pack(pady=5) btn = Button(self.parent, command=self.update, text='Test').pack(side='bottom', pady=10) self.lab = Label(self.parent) self.lab['text'] = '' self.lab.pack(expand=True, fill='x') def update(self): img = PhotoImage(file='images/img2.png') self.label['image'] = img img.image = img self.i = 8 self.timer() def display(self): img = PhotoImage(file='images/img1.png') self.label['image'] = img img.image = img self.lab['text'] = '' ### This will fly through the coundown on the 2nd run # self.lab.after_cancel(self.myvar) ### If I leave out part or all of the arguments the ### timer works correct. Just get errors in the console self.lab.after_cancel(self.lab.after(self.timer)) def run_timer(self): if self.i >= 0: self.lab['text'] = f'Resetting in {self.i}' self.i -= 1 else: self.display() def timer(self): self.run_timer() self.myvar = self.lab.after(1000, self.timer) def main(): root = Tk() Picture(root) root.mainloop() main()