473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1149
lu***********@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
Jul 2 '07 #2
lu***********@gmail.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.invalidwrote:
luke.hoers...@gmail.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
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,...
24
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...
21
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...
1
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...
8
by: Ivan S | last post by:
What are your recommendations for lightweight database abstraction library (Oracle/MySQL)? I prefer OOP. :) Tnx, Ivan.
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.