[Tutor] Tkinter and after() method

Phil phillor9 at gmail.com
Tue Jan 31 20:51:40 EST 2023


On 1/2/23 07:27, Cameron Simpson wrote:
>
> As you suspect, using a global limits your flexibility.
>
> How about having the dial instance do its own ticker loop? Untested 
> example:
Thank you Cameron,
>
> class Dial:
>     def __init__(self, frame, canvasName, x=60, y=60, size=60):
>         self.frame = frame
>         self.canvasName = canvasName
>         self.x = x  # centre of dial
>         self.y = y
>         self.size = size # the diameter of the dial
>         self.xn = self.x + 1 # the end points of the ticks and pointer
>         self.yn = self.y + 1
>         self.value = 0 # the pointer's value
>         self.loop()
>
>     def loop(self):
>         self.value += 10  # change the pointer position by 10%
>         if self.value > 100:
>             self.value = 0
>         move_pointer(self, self.canvasName)
>         self.frame.after(1000, self.loop)
>
> Note that last line: `self.loop` is a reference to the `loop()` method 
> for this particuar dial. The initialisation of the Dial kicks off the 
> first execution of that `loop()`, as you _were_ doing in 
> `Root.__init__` but now should do here, thus once per Dial as made.
>
> Then intialise your dial with the addition of the frame and 
> canvasName, and do that in Root.__init__. That lets you drop the 
> globals entirely.
I see, doesn't that limit the class to only work with Tkinter, and is 
that normal practise? I try to make my classes universal so that they 
can be used with Tkinter and Wxpython. At the moment I'll be happy if 
the Dial class only works with Tkinter.

The basic pattern here is that all the relevant state is stored with the 
Dial, and thus you can independently have many dials.
>
> Frankly, I've have move_pointer() _also_ be a method of Dial.

I'm part way through a frustrating plumbing job on my motorhome so I 
probably won't get back to programming until tomorrow. In the meantime 
I'll give your suggestions some thought. Moving frame and canvasName to 
Dial certainly seems like a good idea.

-- 

Regards,
Phil



More information about the Tutor mailing list