Correct abstraction for TK
Laurent Pointal
laurent.pointal at limsi.fr
Tue Jul 3 03:21:22 EDT 2007
luke.hoersten at gmail.com a écrit :
> 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
>
Maybe try with easygui [1]. The multenterbox [2] seem to be right to
enter 4 values [3]... Its really a nice tool to have a quick and clean
user input solution with GUI in a function.
A+
Laurent.
[1] http://www.ferg.org/easygui/
[2] http://www.ferg.org/easygui/screenshot-multenterbox.png
[3] From the doc:
MULTENTERBOX AND MULTPASSWORDBOX --
GETTING INFORMATION FROM THE USER ABOUT MULTIPLE FIELDS
=======================================================================
Multenterbox is a simple way of showing multiple enterboxes on a single
screen. Multpasswordbox has the same interface as multenterbox, but
when it is displayed, the last of the fields is assumed to be a
password, and is masked with asterisks.
def multenterbox(message="Fill in values for the fields."
, title=""
, argListOfFieldNames = []
, argListOfFieldValues = []
):
"""Show screen with multiple data entry fields.
The third argument is a list of fieldnames.
The the forth argument is a list of field values.
If there are fewer values than names, the list of values is padded
with empty strings until the number of values is the same as the
number of names.
If there are more values than names, the list of values
is truncated so that there are as many values as names.
Returns a list of the values of the fields,
or None if the user cancels the operation.
Here is some example code, that shows how values returned from
multenterbox can be checked for validity before they are accepted.
--------------------------------------------------------------
msg = "Enter your personal information"
title = "Credit Card Application"
fieldNames = ["Name","Street Address","City","State","ZipCode"]
fieldValues = [] # we start with blanks for the values
fieldValues = multenterbox(msg,title, fieldNames)
# make sure that none of the fields was left blank
while 1:
if fieldValues == None: break
errmsg = ""
for i in range(len(fieldNames)):
if fieldValues[i].strip() == "":
errmsg = errmsg + ('"%s" is a required field.\n\n' %
fieldNames[i])
if errmsg == "": break # no problems found
fieldValues = multenterbox(errmsg, title, fieldNames,
fieldValues)
print "Reply was:", fieldValues
------------------------------------------------------------
"""
More information about the Python-list
mailing list