February 25th, 2006, 06:50 PM
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.
February 25th, 2006, 07:02 PM
Have you taken a closer look at the PyDispatcher module from:
Comments on this post
February 25th, 2006, 07:22 PM
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.
February 25th, 2006, 07:26 PM
February 25th, 2006, 07:37 PM
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.
Originally Posted by †Yegg†
I'm not really an IDE fan - more of a CLI/Emacs type of person. Thanks for the suggestion, though.
February 27th, 2006, 09:04 AM
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.
March 1st, 2006, 12:16 PM
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.
Originally Posted by Grim Archon
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).
March 2nd, 2006, 03:50 AM
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.
Originally Posted by Dietrich
Thanks Dietrich and rep to you.
Last edited by Grim Archon; March 2nd, 2006 at 03:54 AM.