Focus problem when opening a Frame via EVT_LIST_ITEM_SELECTED

Hi all,

I want to open a Frame by clicking on an Item in a ListCtrl.
Problem: The Frame opens but does not stay in the front.

But when I open the Frame by clicking on a button or using the right mouse button on the ListCtrl it works as expected!

What am I doing wrong?

I have attached a small example, where you can see the problem…

open_the_frame.py (2.49 KB)

Greetings,

On Win7 32bit, python 2.7.x, wxPython 2.9.5.0 I was able to keep the new frame on top by adding the following line into the Item Selected function.

myFrame2.SetFocus()

Hope this helps,

-Mike S

···

On Tuesday, February 4, 2014 10:31:43 AM UTC-5, Leon wrote:

Hi all,

I want to open a Frame by clicking on an Item in a ListCtrl.
Problem: The Frame opens but does not stay in the front.

But when I open the Frame by clicking on a button or using the right mouse button on the ListCtrl it works as expected!

What am I doing wrong?

I have attached a small example, where you can see the problem…

Thanks Mike,
unfortunately that does not work for me.
I use python 2.7.2, wxPython 2.8.12.1 (but the ansi version) on Vista…

···

Am Dienstag, 4. Februar 2014 18:08:04 UTC+1 schrieb Mike Stover:

Greetings,

On Win7 32bit, python 2.7.x, wxPython 2.9.5.0 I was able to keep the new frame on top by adding the following line into the Item Selected function.

myFrame2.SetFocus()

Hope this helps,

-Mike S

On Tuesday, February 4, 2014 10:31:43 AM UTC-5, Leon wrote:

Hi all,

I want to open a Frame by clicking on an Item in a ListCtrl.
Problem: The Frame opens but does not stay in the front.

But when I open the Frame by clicking on a button or using the right mouse button on the ListCtrl it works as expected!

What am I doing wrong?

I have attached a small example, where you can see the problem…

Leon wrote:

Thanks Mike,
unfortunately that does not work for me.
I use python 2.7.2, wxPython 2.8.12.1 (but the ansi version) on Vista...

Try using wx.CallAfter to defer the create and show of the frame until after the current EVT_LIST_ITEM_SELECTED event has completed.

···

--
Robin Dunn
Software Craftsman

Thanks Robin,
but that does not help for me either!
I have added / changed these lines:

def openFrame2(self):
    myFrame2 = Frame2(self)
    myFrame2.Show()       

def OnListCtrl1ListItemSelected(self, event):
    wx.CallAfter(self.openFrame2)

But the window still disappears behind Frame1, when I click on the List Item…

open_the_frame.py (2.49 KB)

···

Am Mittwoch, 5. Februar 2014 03:35:37 UTC+1 schrieb Robin Dunn:

Try using wx.CallAfter to defer the create and show of the frame until
after the current EVT_LIST_ITEM_SELECTED event has completed.

This is really strange!
I added some more lines to the openFrame2 Funktion:

def openFrame2(self):
    myFrame2 = Frame2.create(self)
    time.sleep(1)
    myFrame2.Show()
    time.sleep(1)
    self.Raise()
    time.sleep(1)
    myFrame2.Raise()
    time.sleep(1)
    self.Raise()
    time.sleep(1)
    myFrame2.Raise()

Now the both Frames happily change focus, but at the end myFrame2 hides behind Frame1!

I have also experimented with wx.EVT_LEFT_UP, but that is not detected without EVT_LEFT_DOWN. And with EVT_LEFT_DOWN the selection does not work!

This is really strange!

No, It is not. You are approaching the problem the wrong way.

Try this. Works fine. Floats on parent style bit like it is supposed to.

···

On Thursday, February 6, 2014 1:54:01 AM UTC-6, Leon wrote:

class Frame2(wx.Frame):
def init(self, parent, id=wx.ID_ANY, title=wx.EmptyString,
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT, name=‘frame’):
wx.Frame.init(self, parent, id, title, pos, size, style, name)

Thank you Metallicow!
This works for me.
But this can not be the real solution, because Frame2 stays on top, even if I click on Frame1…
Nevermind :slight_smile:

···

Try this. Works fine. Floats on parent style bit like it is supposed to.

class Frame2(wx.Frame):
def init(self, parent, id=wx.ID_ANY, title=wx.EmptyString,
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT, name=‘frame’):
wx.Frame.init(self, parent, id, title, pos, size, style, name)

Thank you Metallicow!
This works for me.
But this can not be the real solution, because Frame2 stays on top, even if I click on Frame1…
Nevermind :slight_smile:

Try this. Works fine. Floats on parent style bit like it is supposed to.

class Frame2(wx.Frame):
def init(self, parent, id=wx.ID_ANY, title=wx.EmptyString,
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT, name=‘frame’):
wx.Frame.init(self, parent, id, title, pos, size, style, name)

Try hacking around with this… I had a similar issue with TaskBar Icon/Iconize
You could also try binding wx.EVT_ENTER_WINDOW to SetFocus(); Raise()
Maybe I’m not understanding what type of effect you are wanting.
Adding wx.CallAfter of CallLater with this combined with the frames EVT_ACTIVATE or EVT_ACTIVATE_APP should work fine for raising any frame that is under the mouse.
If anything past this point of custimization is needed, then you are probably looking at writing some new code to handle it.

···

On Thursday, February 6, 2014 9:21:01 AM UTC-6, Leon wrote:

def OnTaskBarActivate(self, event=None):
if wx.VERSION_STRING.startswith(‘2.9’) and not PHOENIX:#BUG…? wx >= wx2.9.4.0 why have to do it twice… probably the order of events.
self.parent.Iconize(False)
self.parent.Show(True)
self.parent.Raise()

        self.parent.Iconize(False)
        self.parent.Show(True)
        self.parent.Raise()
    else:
        if self.parent.IsIconized():
            self.parent.Iconize(False)
        if not self.parent.IsShown():
            self.parent.Show(True)
        self.parent.Raise()
        print('OnTaskBarActivate Raise()')

def BringWindowToFront(self):
try: # It’s possible for this event to come when the frame is closed.
self.GetTopWindow().Raise()
except Exception as exc:
pass

def OnActivateApp(self, event):
    if not event.GetActive(): # Removing this screws up context wx.EVT_HELP/ContextHelp
        return
    # self.BringWindowToFront()

Here is a sample using EVT_LIST_ITEM_ACTIVATED or EVT_LEFT_UP
This will require “Double” Clicking.

Do you really need to use SELECTED?

ListItemActivatedShowFrame.py (3.99 KB)

If you really need to use SELECTED, then change the EVT_LEFT_UP to EVT_LEFT_DOWN in the attached sample app.
In the OnLeftDown def,
you will need to figure out programmatically what the SELECTED event does for you then call the frame depending on what infos you want to send it.

I have tried to use EVT_LEFT_UP and EVT_LEFT_DOWN.
And played around with event.Skip(),
but either the EVT_LIST_ITEM_SELECTED or the EVT_LEFT_UP is not detected.
I have attached open_the_frame_EVT_LEFT_UP.py to demonstrate.

open_the_frame_EVT_ENTER_WINDOW.py (3.01 KB)

open_the_frame_EVT_LEFT_UP.py (3.27 KB)

···

I have also tried to use the EVT_ENTER_WINDOW in order to raise the Frame1 because Frame2 is on top because of FRAME_FLOAT_ON_PARENT.
But FRAME_FLOAT_ON_PARENT seems to be stronger :slight_smile:
I have attached open_the_frame_EVT_ENTER_WINDOW.py to demonstrate.