Correct abstraction for TK
Adonis Vargas
adonis at REMOVETHISearthlink.net
Mon Jul 2 18:33:28 EDT 2007
luke.hoersten at gmail.com wrote:
> I'm looking for a good example of how to correctly abstract TK code
> from the rest of my program. I want to just get some user info and
> then get 4 values from the GUI. Right now I've written it OOP per the
> examples on python.org but it doesn't seem to be meshing very well
> with the rest of my project.
>
> Can anyone suggest some examples so that I can get more ideas?
>
> Thanks,
> Luke
>
I would not consider this to be 'correct' as many have different
philosophies on how to tackle certain projects. My way of doing
programming with GUI is writing it in 'tiers' (using this word loosely).
i.e.
import Tkinter as tk
class UsersInfo:
pass
class Events(UserInfo):
pass
class GUI(Events):
pass
This way your GUI class sends events to the Events class to act upon the
UserInfo class. For the GUI class here all you do is code the actual
display and the callbacks only. Then in the Events class you code the
actions you want to happen when you interact the the GUI. Since the GUI
class inherits the Events class, in the GUI class you would simply call
a method found in the Events class when an event is triggered. Now the
Events class which inherits the UserInfo class, you can start using the
class to store/modify the user data you desire. Now your code is
separated into more comprehensible, and easier to manage sections. In
this example I am using inheritance, but if you prefer delegation, then
that too can be done here. Also, by doing this it will simplify the
moving to more robust graphic toolkits with little modification.
Hope this helps.
Adonis
More information about the Python-list
mailing list