In my current project, I need UI elements to enable/disable in
response to whether there is text entered in various TextFields.

As I couldn't see any built-in way for PyGUI to trigger an action
based on a change in TextField content, unless the user hits enter or
escape. One option would be to have a big "update" button for the user
to click, but this isn't very practical - people expect programs to be
more dynamic these days.

To get around this, I've subclassed TextField to essentially trigger
the enter action after any keystroke that changes the value of its
text. Simplified code is below:

class MyTextField(TextField):
        def key_down(self, event):
                oldtext = self.get_text()
                TextField.key_down(self, event)
                if oldtext != self.get_text():
                        self.do_enter_action()

This works fine on Mac OS X, but not on Windows, where the value
returned by self.get_text() doesn't seem to change following the call
to the SuperClass. This invariably means that oldtext ==
self.get_text() in the above code. However, if I then hit another key,
or click another UI element, I can see (via debug printing) that
get_text *has* changed, at some point between the previous key_down
finishing and the action being sent.

If I remove the if test and just directly call do_enter_action, the
action that is triggered (which in turn calls the textfield's
get_text() function) again returns the pre-keypress value. As this
action is used to update the model, it means the model data is always
one keypress behind the UI!

I'm guessing from this behaviour that the value of the text itself is
only updated when the widget redraws?

Anyway, two questions:

1) Is there an easier way of doing this? (responding to textfield
input without waiting for an enter key)
2) If not, any guesses on how to fix the above?

Cheers,

Alex
_______________________________________________
Pygui mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pygui

Reply via email to