strange behaviour with EVT_KILL_FOCUS and wx.MessageBox

Hi,

I have a little problem with wx.EVT_KILL_FOCUS:

I want to show a MessageBox after leaving a Text-Entry field.

I have two wx.TextCtrl and each one has wx.EVT_KILL_FOCUS binded to them.

When I run the example program:

  • click into the first field
  • then press TAB to go to the next field
  • MessageBox is displayed
  • Press Enter in order to get to close the MessageBox
    –> the second MessageBox is displayed, although I did not leave the second text field!

If I just print the message to the console it works as expected.

What am I doing wrong?

python version: Python 2.7.12
wx.python version: 3.0.2.0 gtk2 (classic)
running on 16.04. Kubuntu (with plasma). Uname gives: 4.4.0-34-generic #53-Ubuntu

example_EVT-kill.py (1.65 KB)

Leon wrote:

I have a little problem with wx.EVT_KILL_FOCUS:

I want to show a MessageBox after leaving a Text-Entry field.

I have two wx.TextCtrl and each one has wx.EVT_KILL_FOCUS binded to them.
...
If I just print the message to the console it works as expected.

What am I doing wrong?

Focus is a complicated topic. Remember, when you display your message
box, the app itself loses focus. Windows ordinarily will not sent a
WM_KILLFOCUS to a focused control within an app losing focus, but I
believe wx does.

So, I think the processing goes something like this. When you tab out
of text1, it sets the focus to text2 and queues up a "lost focus" event
for text1. During the process of handling that event, you put up a
message box, which causes the newly-focused text2 to get a "lost focus"
event.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hi,

Leon wrote:

I have a little problem with wx.EVT_KILL_FOCUS:

I want to show a MessageBox after leaving a Text-Entry field.

I have two wx.TextCtrl and each one has wx.EVT_KILL_FOCUS binded to them.
...
If I just print the message to the console it works as expected.

What am I doing wrong?

Focus is a complicated topic. Remember, when you display your message
box, the app itself loses focus. Windows ordinarily will not sent a
WM_KILLFOCUS to a focused control within an app losing focus, but I
believe wx does.

So, I think the processing goes something like this. When you tab out
of text1, it sets the focus to text2 and queues up a "lost focus" event
for text1. During the process of handling that event, you put up a
message box, which causes the newly-focused text2 to get a "lost focus"
event.

To solve it you could use:

wx.CallAfter()

function call.

Thank you.

···

On Thu, Aug 25, 2016 at 12:27 PM, Tim Roberts <timr@probo.com> wrote:

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Igor Korot wrote:

···

On Thu, Aug 25, 2016 at 12:27 PM, Tim Roberts <timr@probo.com> wrote:

Focus is a complicated topic. Remember, when you display your message
box, the app itself loses focus. Windows ordinarily will not sent a
WM_KILLFOCUS to a focused control within an app losing focus, but I
believe wx does.

So, I think the processing goes something like this. When you tab out
of text1, it sets the focus to text2 and queues up a "lost focus" event
for text1. During the process of handling that event, you put up a
message box, which causes the newly-focused text2 to get a "lost focus"
event.

To solve it you could use:

wx.CallAfter()

function call.

I don't see how that changes anything. He'll still get a "lost focus"
event when the message box comes up. I'm afraid the real answer is that
he'll need to manage more state. Like, only issue the message box if
the text field was changed since it got focus.

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hi,
CallAfter did not help me. If I implemented it correctly? (see attachment…)

example_EVT-kill_CallAfter.py (1.72 KB)

···

Am Donnerstag, 25. August 2016 23:44:13 UTC+2 schrieb Tim Roberts:

Igor Korot wrote:

On Thu, Aug 25, 2016 at 12:27 PM, Tim Roberts ti...@probo.com wrote:

Focus is a complicated topic. Remember, when you display your message

box, the app itself loses focus. Windows ordinarily will not sent a

WM_KILLFOCUS to a focused control within an app losing focus, but I

believe wx does.

So, I think the processing goes something like this. When you tab out

of text1, it sets the focus to text2 and queues up a “lost focus” event

for text1. During the process of handling that event, you put up a

message box, which causes the newly-focused text2 to get a “lost focus”

event.

To solve it you could use:

wx.CallAfter()

function call.

I don’t see how that changes anything. He’ll still get a “lost focus”

event when the message box comes up. I’m afraid the real answer is that

he’ll need to manage more state. Like, only issue the message box if

the text field was changed since it got focus.


Tim Roberts, ti...@probo.com

Providenza & Boekelheide, Inc.

Hi,
the other solution works for me: only calling the MessageBox after the text was changed.
See attached file :slight_smile:
Thank you!

example_EVT-kill_only-after-change.py (2.09 KB)

···

Am Donnerstag, 25. August 2016 23:44:13 UTC+2 schrieb Tim Roberts:

Igor Korot wrote:

On Thu, Aug 25, 2016 at 12:27 PM, Tim Roberts ti...@probo.com wrote:

Focus is a complicated topic. Remember, when you display your message

box, the app itself loses focus. Windows ordinarily will not sent a

WM_KILLFOCUS to a focused control within an app losing focus, but I

believe wx does.

So, I think the processing goes something like this. When you tab out

of text1, it sets the focus to text2 and queues up a “lost focus” event

for text1. During the process of handling that event, you put up a

message box, which causes the newly-focused text2 to get a “lost focus”

event.

To solve it you could use:

wx.CallAfter()

function call.

I don’t see how that changes anything. He’ll still get a “lost focus”

event when the message box comes up. I’m afraid the real answer is that

he’ll need to manage more state. Like, only issue the message box if

the text field was changed since it got focus.


Tim Roberts, ti...@probo.com

Providenza & Boekelheide, Inc.