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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /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
Download my project scripts


Reply
#2
I got it working now with these changes. I done away with the after_cancel and just used the timer itself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /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
Download my project scripts


Reply


Forum Jump:

User Panel Messages

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