[Tutor] Tkinter and after() method

Alan Gauld alan.gauld at yahoo.co.uk
Wed Feb 1 12:45:46 EST 2023


On 01/02/2023 01:51, Phil wrote:

>> 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 usual way to deal with multiple UI toolkits(including web
and command line!) is to adopt an MVC pattern. Then you have
the data object and its representation (as a dial?) as
two separate (but related) classes. (MVC has other advantages
too if its a non trivial GUI but for simple apps its not
necessary.)

class SomeDataObjectWithAValue:....

class Dial(Window):  # or maybe inherit some other control?
   # abstract class with methods required to display the value
   def __init__(self, theValueObject, size, location, colour,...):...
   def draw(self):...
   def update(self):...
   ...other Dial methods...


class TkDial(Dial):
   # Tkinter version of a dial

class WxDial(Dial):
   # wxPython version of a dial

Then your program uses the Dial object in its code but
somewhere in its initialisation has something like

# figure out which widget set is in use
if widgets == Tkinter:    # or just hard code it!
   Dial = TkDial
else
   Dial = WxDial

But that's getting pretty complex and a lot of exta work,
especially if there are multiple widgets, all needing twin
versions! So most projects just pick a widget set and run
with it.

The real issue here is that a Dial is a representation of
something else. It's just a way of displaying a value.
The thing that should be kept free of GUI widget awareness
is the underlying value (object) that the dial is displaying.

-- 
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