Looking for sample python script using Tkinter

Christian Gollwitzer auriocus at gmx.de
Fri Jan 2 18:26:12 EST 2015


Am 03.01.15 um 00:03 schrieb accessnewbie at gmail.com:
> I have a script that I trying to go from command line entry to interface entry. I am tinkering with Tkinter and want to review some Tkinter interface building scripts. Really basic stuff covering file selection and making some of the data captured required I am learning how to use Tkinter (Python 2.7) to build a data entry interface where the variables (about 15 data entry fields in all) will be used later in the script to perform some analyses. I have a few requirements when capturing the data:
>
>  1   Must be able to navigate to a file and capture entire filename and pathname (which may be on a network rather than the C drive)
>  2   Capture date
>  3   Capture text (Some of the data entry fields will have commas)
>  4   Some of the data entry fields are required, some are not.
>
> Is there a sample script out there that I can review to see how
> these  features are accomplished? I am particularly stumped by #1 and 4.

1: Does tkFileDialog.askopenfilename() fulfill your needs? On Windows, 
this should open the native file dialog. Usually, one packs a button 
near an entry widget to open the file dialog and the return value is 
placed in the entry

2: There are some date megawidgets. But you might prefer to just have an 
entry and validate the input / convert it to a date

3: The basic entry widget should do. It has no problems with commas. the 
only critical character is a tab, because it switches to the next field

4: GUI widgets don't have the notion of being required or not, that is 
entirely up to your application. Usually, there will be an "Apply" or 
"Submit" button or similar (or the action is bound to the Return key). 
On that occasion, don't accept the data if the required fields are left 
blank.

In general, you need to validate all input fields before you accept 
them. This is specific to your application and can't be handled by the 
toolkit itself. For common tasks like single real numbers, there are 
ready-made validators available that can validate the input already 
during editing. Note this is more difficult to get right than it seems 
at first glance; e.g. copy/pasting can be damaged if the validator is 
too simple. It is much easier to validate only before the data is accepted.

There are also several choices how you react if the validation fails. 
You could show the error in a popup (tkMessageBox). This is the simplest 
approach, but makes the error unavailable to the user after it is 
acknowledged. A better method from a usability perspective is a status 
line that signals the error, possibly in red, and also marks the fields 
with errors in red. All of this can be done in a couple of lines easily, 
but is lots of work to do completely right and capture every possible case.

	Christian



More information about the Python-list mailing list