TextCtrl focus events in wxWidgets

S

Simon Hibbs

I have a simple form with some input values and some calculated values
in TextCtrl widgets.

What I would like to do is have the display update automaticaly when
the user changes one of the input fields, without having to click on a
'Calculate' button. I was thinking of having an update triggered when
one of the text Controlls loses focus, indicating that the user has
finished changing it's value.

I don't want to do an update on every character entry, as this would
create a lot of bogus/meaningless updates. There doesn't seem to be
such an event. Any ideas how I could implement this, or a similarly
user friendly behaviour?

Best regards,

Simon Hibbs
 
S

Steve Holden

Simon said:
I have a simple form with some input values and some calculated values
in TextCtrl widgets.

What I would like to do is have the display update automaticaly when
the user changes one of the input fields, without having to click on a
'Calculate' button. I was thinking of having an update triggered when
one of the text Controlls loses focus, indicating that the user has
finished changing it's value.

I don't want to do an update on every character entry, as this would
create a lot of bogus/meaningless updates. There doesn't seem to be
such an event. Any ideas how I could implement this, or a similarly
user friendly behaviour?
It should be quite simple: you need to handle EVT_SET_FOCUS and/or
EVT_KILL_FOCUS events (documented in the wxPython docs) to know when to
recaclulate the values. Sounds like that should be enough of a hint to you.

regards
Steve
 
S

Simon Hibbs

Steve said:
It should be quite simple: you need to handle EVT_SET_FOCUS and/or
EVT_KILL_FOCUS events (documented in the wxPython docs) to know when to
recaclulate the values. Sounds like that should be enough of a hint to you.

I've tried that, but it doesn't work. Here is the test code:

self.PlantCtrl = wx.TextCtrl(self, -1, "")

self.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl,
self.PlantCtrl)

def OnUpdatePlantCtrl(self, event):
print "set Plant"

When the control loses focus, I don't get the message in the console.
I'm trapping other events successfuly elsewhere using similar code.

Simon Hibbs
..
 
S

Simon Hibbs

rony said:
Since the event handler of a textctrl inherits from wxCommandEvent,
I would guess that the binding should be to EVT_COMMAND_KILL_FOCUS

Still not working :(

Simon
 
S

Simon Hibbs

Simon said:
Still not working :(

I can trap EVT_TEXT_ENTER events successfuly, without using
EVT_COMMAND_ENTER. This almost gets me where I want to be. The user
must press 'enter' after modifying each value though. If they forget
the UI isn't updated, so I'd need some way of visualy distinguishing
between modified values that have been ENTER'd and those that haven't
which is a pain, and not very user friendly at all.

There must be some way of doing this, but blowed if I can figure it
out.

Simon
 
F

Frank Millman

Simon said:
I've tried that, but it doesn't work. Here is the test code:

self.PlantCtrl = wx.TextCtrl(self, -1, "")

self.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl,
self.PlantCtrl)

def OnUpdatePlantCtrl(self, event):
print "set Plant"

When the control loses focus, I don't get the message in the console.
I'm trapping other events successfuly elsewhere using similar code.

Simon Hibbs

Try self.PlantCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl)

Frank Millman
 
S

Steve Holden

Simon said:
Steve Holden wrote:




I've tried that, but it doesn't work. Here is the test code:

self.PlantCtrl = wx.TextCtrl(self, -1, "")

self.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl,
self.PlantCtrl)

def OnUpdatePlantCtrl(self, event):
print "set Plant"

When the control loses focus, I don't get the message in the console.
I'm trapping other events successfuly elsewhere using similar code.
This would probably be a good question for the wxPython list then - you
are clearly in some little-known area of swamp ;-)

regards
Steve
 
S

Simon Hibbs

Frank said:
Try self.PlantCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl)

And Voila! It works. Many, many thanks.

Any idea what is going on?

Simon
 
D

David Hughes

Simon said:
And Voila! It works. Many, many thanks.

Any idea what is going on?

AIUI, wx.EVT_KILL_FOCUS is not a Command Event i.e. it doesn't
propagate up the hierarchy of widgets until it gets handled, so it has
to be bound explicitly to the control itself, as above.

Originally you used:

self.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl, self.PlantCtrl)

which binds the event to self (presumably the containing frame or
panel) and adds the condition that it must come from self.PlantCtrl -
which never happens.
 
P

Peter Decker

And Voila! It works. Many, many thanks.

Any idea what is going on?

Your first attempt used self.Bind, which binds the kill focus event of
self to the method. This version binds the kill focus event of the
*text control* to the method, which is what you want.

I used to get bitten by this a lot, but now I've switched to using the
dabo.ui module of Dabo to do my GUI stuff. It has the concept of
binding changes in controls to update events, which was needed for
database-type apps, but you can bind a control to any property of any
object. You should really check it out if you need this sort of
interactive updating in your app. http://dabodev.com.
 
R

rony steelandt

Since the event handler of a textctrl inherits from wxCommandEvent,
I would guess that the binding should be to EVT_COMMAND_KILL_FOCUS

Not tested...


Rony



Le Wed, 19 Jul 2006 03:15:36 -0700, Simon Hibbs a écrit :
 
F

Frank Millman

Simon said:
And Voila! It works. Many, many thanks.

My pleasure
Any idea what is going on?

I only understand it in simple terms, though it can get complex. Here
is my simple explanation.

Events are received by objects. There are default event handlers that
are called to deal with the events. If you want your own event handler
to be called, you use Bind(), which brings together three elements -
the window receiving the event (in wxPython, all objects derive from
wx.Window), the event itself, and the event handler to be called.

In pseudo code, you call Bind() like this -

w = the window receiving the event
e = the event
h = the handler to be called

w.Bind(e,h)

You bound EVT_KILL_FOCUS to self, which I assume in your case is a
panel, but the panel does not receive the KILL_FOCUS event, the text
control does.

For more information, type help(wx.Window.Bind) at the interpreter
prompt.

If you want an authoritative answer, post a question to the wxPython
mailing list. Robin Dunn, the creator of wxPython and the resident
guru, is always happy to explain in detail exactly what is going on.
You can also get Robin's book, wxPython In Action.

Frank
 
F

Frank Millman

David said:
AIUI, wx.EVT_KILL_FOCUS is not a Command Event i.e. it doesn't
propagate up the hierarchy of widgets until it gets handled, so it has
to be bound explicitly to the control itself, as above.

Right.

This is one of the sources of confusion with events - which ones
'propagate up the hierarchy' and which ones do not.

This is a quote from the wxWidgets documentation -

"Typically events that deal with a window as a window (size, motion,
paint, mouse, keyboard, etc.) are sent only to the window. Events that
have a higher level of meaning and/or are generated by the window
itself, (button click, menu select, tree expand, etc.) are command
events and are sent up to the parent to see if it is interested in the
event."

Simon's first attempt would have been correct if EVT_KILL_FOCUS was
treated as a Command Event. Unfortunately, it is not, and therefore it
was not passed up to 'self' for handling.

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top