#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0

    GUI program design - communicating between objects.


    I've been programming for a number of years but only recently started using GUI toolkits and am now working on my first moderate-sized GUI based program. I've run into some problems with the program design and am looking for some advice. I think this applies whatever language and toolkit you use, but in my case I'm using Python and TkInter - hence posting in this forum. If someone thinks this would be better asked elsewhere please tell me.

    My problem stems from the fact that the main event loop in this type of program is within the GUI toolkit, meaning that (as far as the main program is concerned) execution can start in sub-modules without them having been called from a higher level module (I use "module" in a general sense here). This is not the case with the type of programs I have written in the past, where the main event loop (or loops) were within my code, and thus sub-modules only executed when called.

    Consider (in the simplest case) a program made up of three objects. It has a top level App object, which instantiates two objects A and B. Each of these objects has some GUI widgets associated with it and can thus generate events (e.g. a callback when a button is pressed).

    If an event generated by object A needs to cause a change to object B, what's the best way of communicating this? I could pass one of B's methods (or a reference to B as a whole) to A, but (1) this means that A and B are more closely coupled than I would like, and (2) this doesn't scale well to the situation where you have many objects and several levels of hierarchy.

    The scheme I'm currently using is a sort of message passing - I'm defining a "message handler" method in the top level App object and passing a reference to it to all other objects. An object wishing to notify other objects of an event calls it with a message name and (if required ) a set of parameters, and the App "message handler" method then determines which methods to call in the other objects.

    This works, but seems messy (at least the way I'm doing it) and I'm wondering if either there's a better way, or at least some guidelines on keeping it from being messy.

    Thanks

    Andy
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Have you taken a closer look at the PyDispatcher module from:
    http://cheeseshop.python.org/pypi/PyDispatcher/1.0.2

    Comments on this post

    • Grim Archon agrees : Useful link - thanks :)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
    Thanks for the quick reply. PyDispatcher looks interesting, and may well be a suitable solution to my problem.

    Can anyone tell me is this the approach normally used when developing larger GUI apps? Or are they normally built on top of some kind of framework which handles this type of thing? I'm not just thinking Python now, but GUI apps in general, though Python is the language I'm most interested in using.

    Thanks

    Andy
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I'd go with using wxPython and use Boa Constructor as your drag-and-drop visual environment.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
    Originally Posted by †Yegg†
    I'd go with using wxPython
    I've been thinking of giving wxPython a try at some point, but my question is really more about the general program design approach than about using a particular GUI toolkit.
    and use Boa Constructor as your drag-and-drop visual environment.
    I'm not really an IDE fan - more of a CLI/Emacs type of person. Thanks for the suggestion, though.

    Andy
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Tkinter/wxPython use widget events which you associate with functions/methods. From your codes perspective both frameworks basically do nothing until events happen (Timer, button click, key press, redraw, user defined etc).

    This is how all the GUIs I have used schedule activity (in other frameworks it may be called something different e.g. signals and slots but the principle remains the same). You let the framework do all the monitoring and routing, your code is responsible for the triggers and the handlers.

    It requires a different way of thinking to traditional console apps - you don't busy wait.

    grim
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
    Originally Posted by Grim Archon
    Tkinter/wxPython use widget events which you associate with functions/methods. From your codes perspective both frameworks basically do nothing until events happen (Timer, button click, key press, redraw, user defined etc).
    Yes - I understand how it works. I have written several smaller programs (1000 lines or less) with Tkinter and am now writing a moderate sized (~3500 lines so far) program using it. My question was to do with a good way of communicating between objects - a problem that could exist in any type of program but is particularly apparent with GUI based designs where events can originate within an object without it (or its methods) being called from a higher level object.

    PyDispatcher looks to me to be the way to do this - once I have time to figure out how to use it. I have a deadline of this Saturday to have a basic version of the program working so I'm going to stick with my home-grown message passing system for the moment, but hopefully after that I'll switch it over to use PyDispatcher.

    I do have some other questions about good GUI program design principles and techniques but I'll raise them in separate threads at some point (if i can't find anything already addressing them).

    Thanks again

    Andy
  14. #8
  15. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Originally Posted by Dietrich
    Have you taken a closer look at the PyDispatcher module from:
    http://cheeseshop.python.org/pypi/PyDispatcher/1.0.2
    Actually looks very useful I have been planning a project that needed a messaging system and was going to use a base class with signals and slots and a messaging hub - I think pydispatcher will be a good alternative solution for me.

    Thanks Dietrich and rep to you.

    grim
    Last edited by Grim Archon; March 2nd, 2006 at 02:54 AM.

IMN logo majestic logo threadwatch logo seochat tools logo