473,837 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Correct abstraction for TK

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

Jul 2 '07 #1
3 1172
lu***********@g mail.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
Jul 2 '07 #2
lu***********@g mail.com writes:
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.
Simplest: just have gui operations call the application code. The
application main loop is just the gui event loop. Example (first tk
program I ever wrote, and one of my first python programs):

http://www.nightsong.com/phr/python/calc.py

That might be enough for what you're doing.

Fancier: put gui in separate thread. Be careful, it's not reentrant;
all communication with the application has to be through queues, sort
of like writing a miniature web server. Most straightforward is to
pass tuples like (function, args, **kwargs) through the queues, where
the opposite end invokes the function on the arg list. There are some
recipes in the Python cookbook for triggering the event loop on a
periodic timeout from inside tkinter.

See also "model-view-controller" for a more complex design approach
intended to separate the interface from the application logic.

Finally, consider total separation by embedding an http server in the
application, so the gui is a web browser and you write a web app.
It's often easier to code a simple html interface than to mess with
the damn Tk widgets and trying to get them to look decent on the
screen, plus it lets you easily put the client on a remote machine,
support multiple clients simultaneously, etc.
Jul 3 '07 #3
Thanks for all the suggestions guys. I'm realizing that I need to
chose more of a specific paradigm. With closures, I was able to stay
away from unneeded classes before but Tk brings it to a whole other
level.

Thanks again,
Luke

On Jul 3, 2:50 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
luke.hoers...@g mail.com writes:
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.

Simplest: just have gui operations call the application code. The
application main loop is just the gui event loop. Example (first tk
program I ever wrote, and one of my first python programs):

http://www.nightsong.com/phr/python/calc.py

That might be enough for what you're doing.

Fancier: put gui in separate thread. Be careful, it's not reentrant;
all communication with the application has to be through queues, sort
of like writing a miniature web server. Most straightforward is to
pass tuples like (function, args, **kwargs) through the queues, where
the opposite end invokes the function on the arg list. There are some
recipes in the Python cookbook for triggering the event loop on a
periodic timeout from inside tkinter.

See also "model-view-controller" for a more complex design approach
intended to separate the interface from the application logic.

Finally, consider total separation by embedding an http server in the
application, so the gui is a web browser and you write a web app.
It's often easier to code a simple html interface than to mess with
the damn Tk widgets and trying to get them to look decent on the
screen, plus it lets you easily put the client on a remote machine,
support multiple clients simultaneously, etc.

Jul 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2192
by: Ruby Tuesday | last post by:
Which one is better to do dynamic websites using MySQL? Thanks a.. ADODB, http://php.weblogs.com/ADOdb/ b.. Metabase, http://www.phpclasses.org/browse.html/package/20.html c.. PEAR::DB, http://pear.php.net/manual/en/core.db.php d.. PHPLib database wrappers, http://sourceforge.net/projects/phplib
24
2577
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office if you don't have Word. Classes <url: http://home.no.net/dubjai/win32cpptut/w32cpptut_02_01.zip> Introduces the C++ language feature used to define new types, namely classes. The focus in on creating safe and reusable classes. As a main
21
3418
by: ambika | last post by:
Hello, I have a very basic doubt. Why is C called a structured programming language??why structured? C++ is called a Object Oriented language 'cos it obeys the OOP's concepts..Why is C called a structured programming lang?? Thanks to all those who are gonna answer.. -ambika
1
1682
by: rickycornell | last post by:
Greetings All, On past projects in PHP4 I had always just written my own libraries to deal with database interaction. Somehow I was operating in the dark that there were all these database abstraction things available that made my work on the libraries I made from scratch a waste of time. So now I've started researching all these things that are available, and I have to say I am a little confused. It's a matter of understanding the...
8
1845
by: Ivan S | last post by:
What are your recommendations for lightweight database abstraction library (Oracle/MySQL)? I prefer OOP. :) Tnx, Ivan.
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10902
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7824
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7014
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5680
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.