Python Forum
[Tkinter] After_cancel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] After_cancel
#1
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
I got it working now with these changes. I done away with the after_cancel and just used the timer itself.
#! /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'] = None
        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'] = None




    def run_timer(self):
        if self.i >= 0:
            self.i -= 1

    def timer(self):
        self.run_timer()
        if self.i >= 0:
            self.myvar = self.lab.after(1000, self.timer)
            self.lab['text'] = f'Resetting in {self.i}'
            self.lab['bg'] = 'burlywood'
            self.lab['fg'] = 'brown'
            self.lab['font'] = 'serif 10 normal'
        else:
            self.myvar = None
            self.lab['text'] = ''
            self.lab['bg'] = 'grey86'
            self.display()



def main():
    root = Tk()

    Picture(root)
    root.mainloop()

main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020