By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

TextCtrl focus events in wxWidgets

P: n/a
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

Jul 19 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Simon Hibbs wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 19 '06 #2

P: n/a

Steve Holden wrote:
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
..

Jul 19 '06 #3

P: n/a
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*:
>
Steve Holden wrote:
>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
.
Jul 19 '06 #4

P: n/a

rony steelandt wrote:
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

Jul 19 '06 #5

P: n/a

Simon Hibbs wrote:
rony steelandt wrote:
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 :(
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

Jul 19 '06 #6

P: n/a

Simon Hibbs wrote:
Steve Holden wrote:
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
Try self.PlantCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl)

Frank Millman

Jul 19 '06 #7

P: n/a
Simon Hibbs wrote:
Steve Holden wrote:

>>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.
This would probably be a good question for the wxPython list then - you
are clearly in some little-known area of swamp ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 19 '06 #8

P: n/a

Frank Millman wrote:
Try self.PlantCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl)
And Voila! It works. Many, many thanks.

Any idea what is going on?

Simon

Jul 19 '06 #9

P: n/a
Simon Hibbs wrote:
Frank Millman wrote:
Try self.PlantCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl)

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.

--
Regards
David Hughes

Jul 19 '06 #10

P: n/a
On 19 Jul 2006 04:55:24 -0700, Simon Hibbs <si*********@gmail.comwrote:
>
Frank Millman wrote:
Try self.PlantCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl)

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.

--

# p.d.
Jul 19 '06 #11

P: n/a

Simon Hibbs wrote:
Frank Millman wrote:
Try self.PlantCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnUpdatePlantCtrl)

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

Jul 19 '06 #12

P: n/a

David Hughes wrote:
>
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

Jul 19 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.