[Tutor] Tkinter and after() method
Alan Gauld
alan.gauld at yahoo.co.uk
Tue Jan 31 20:10:00 EST 2023
On 31/01/2023 05:46, Phil wrote:
> This is the analogue dial class as it is at the moment:
>
> class Dial:
> def __init__(self, x=60, y=60, size=60):
>
> 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
Where are the methods?
An object that has no operations is just data...
> my_dial = None # I cannot see an alternative to their use at the moment
> my_canvas = None
Why not make them attributes of your root class?
After all the Root class actually represents your
application does it not?
> class Root(tk.Tk):
> def __init__(self):
> super().__init__()
> self.title("Canvas Template")
>
> self.frame = tk.Frame(self, background='cornflowerblue')
> self.frame.pack(side='top', fill='both', expand=True)
>
> global my_frame, my_dial, my_canvas
> my_frame = self.frame
>
> self.canvas = tk.Canvas(
> self.frame, relief='flat', background='lightgrey')
>
> self.canvas.pack(side='top', anchor='center', fill='both',
> expand=True, padx=10, pady=10)
>
> my_canvas = self.canvas
>
> self.quit_button = tk.Button(self, text="Quit", command=self.quit)
> self.quit_button.pack(side=tk.BOTTOM, anchor='se', padx=10,
> pady=10)
>
> self.dial = dial.Dial(180, 160, 120)
>
> my_dial = self.dial
>
> draw_dial(self.dial, self.canvas)
>
> move_pointer(self.dial, self.canvas)
>
> loop()
>
>
> def draw_dial(the_dial, canvasName):
Why is this not a method of the dial class. "the_dial" being self...
And canvasName is not really a name but a canvas so why not call it so:
class Dial:
def __init__(...)...
def draw(self,aCanvas):...
> def move_pointer(the_dial, canvasName):
> # this is just a test. In real use the_dial.value woule be supplied
> with
> # a value from a serial device
Again a method of dial...
> def drawPointer(x_centre, y_centre, x_end, y_end, fill, canvasName):
Again a Dial method.
The coordinates should be known to the dial hopefully?
> def loop():
> # again, this only for testing
But could be a method of Root?
Then your globals become
self.my_dial etc.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list