[Tutor] Help me make it look pretty!

Michael Lange klappnase at freenet.de
Fri Jul 21 11:35:32 CEST 2006


On Thu, 20 Jul 2006 20:16:58 +0100
"John CORRY" <john.corry at ntlworld.com> wrote:

> Good evening all.
>  
> I am writing a program using python 2.4, glade 2 and pygtk.  It takes
> input from the user using textentry boxes.  The input should be a
> number.  When the user keys the data in, it automatically left
> justifies.  The function below, takes the input for four boxes and right
> justifies it by using an ugly, string format.  The boxes are 45
> characters big, so I use the len function to fit them into the box.  The
> code is below.
>  
> I am looking for a way to set the text entry up, so that there is a
> decimal point in the box regardless of what the user does and I am also
> looking for a way to keep the numbers right justified.
>  
> Any suggestions or comments as always are greatly appreciated.
>  

Hi John,

I don't know about gtk, but have made some experience validating the user input
in Tkinter Entries, so I can only try to give you some general advice.
First, the Tkinter.Entry has a configuration option "justify", so
entry.config(justify=RIGHT) would very simply solve the first problem; are
you sure the gtk entry box does not have something similar?
Second, I found validating (and especially "on-the-fly"-converting) user input
is always a very tricky thing. Usually when testing some "solution" that seemed
to work, I found that in some special cases the widget showed an completely unexpected
behavior. Generally it is safer not to restrict user input too much and do the validation
when the entry boxes' contents are actually needed; if it is not valid you can pop up
a message box that tells the user what to do. If you really need to perform "real time" validation,
make sure to test your widgets very carefully.
Again, I don't know about gtk, in Tkinter you can pass a "validatecommand" to the entry widget to which
you can pass the old and the new entry's contents,
that is called each time the user types something into the entry box. If gtk offers something similar,
you could start with something like:

# pseudo code
def validate(old, new):
    x = text38.get_text()
    if x in ('', '.'):
        ok = 1
    elif '.' in x:
        try:
            float(x)
            ok = 1
        except ValueError:
            print '\a'#acoustic feedback, in Tkinter I would use bell() of course
            ok = 0
    else:
        try:
            int(x)
            text38.set_text('.'+x)#don't know how this would actually look in gtk, I hope you know what I mean
            ok = 1
        except ValueError:
            print '\a'
            ok = 0
    if not ok:
        # the new text is not allowed, restore the old
        text38.set_text(old)

I bet this kind of solution has its traps, too.
Maybe the best solution is a web search if someone else has already written such a widget.
A quick google search for "pygtk entry validation" for example lead me here:

http://www.astro.umass.edu/~dpopowich/python/ :

ValidatedEntry, a pygtk extension providing a validated Entry widget

ValidatedEntry is a subclass of gtk.Entry, providing validation of input based on programmable functions. Sample functions included in the package provide validation for integers, floats, non-empty strings, ISBN numbers, phone numbers, money and bounded values (e.g., integers in a range). A demo app is included in the package. The latest version, 1.0.4, can be downloaded here.

Good luck!

Michael


More information about the Tutor mailing list