wx.Frame.Show() does not set focus (activate window)

2,287 views
Skip to first unread message

kakarukeys

unread,
Nov 19, 2009, 11:30:23 AM11/19/09
to wxPython-users
Hi All,

I ran into a strange problem to which I don't have a solution. I am
writing a systray application which minimizes itself to systray on
program startup. When binding events to

self.Show()
self.Iconize(False)

ONLY IF the event has special ID such as wx.ID_OPEN, then window will
be activated. If the event has wx.ID_ANY, the window will show and un-
minimize itself, but will not be on focus. This happens to the first
time the event is sent, after that, the behavior returns to normal,
i.e. window will always be on focus.

I'm using

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit
(Intel)] on win32
wxPython 2.8.10.1 (msw-unicode)

Does anyone have an idea how to make the window on focus once the
event is sent?


Code is as follows, please take any ico file as the py.ico.
Double-clicking on the icon (event id = wx.ID_ANY will not set the
window on focus.
Right-click on the icon and Open (event id = wx.ID_OPEN will set the
window on focus.
Although both events are bound to a same method.

import wx

class TestApp(wx.App):
def OnInit(self):
frame = View(parent=None, id=wx.ID_ANY, title="Testing")
frame.SystrayIcon.SetIcon(wx.Icon('py.ico', wx.BITMAP_TYPE_ICO))
frame.Hide()

self.SetTopWindow(frame)
return True

class View(wx.Frame):
"""The main window"""
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)

self.SystrayIcon = TestTaskBarIcon()

btn = wx.Button(self, id=wx.ID_OK)

self.SystrayIcon.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnMenuOpen)
self.SystrayIcon.Bind(wx.EVT_MENU, self.OnMenuOpen, id=wx.ID_OPEN)

def OnMenuOpen(self, event):
self.Show()
self.Iconize(False)

class TestTaskBarIcon(wx.TaskBarIcon):
def __init__(self):
wx.TaskBarIcon.__init__(self)

self.menu = wx.Menu()
self.menu.Append(wx.ID_OPEN, '&Open')
self.menu.AppendSeparator()
self.menu.Append(wx.ID_EXIT, '&Exit')

self.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.ShowMenu)

def ShowMenu(self, event):
self.PopupMenu(self.menu)

if __name__ == "__main__":
app = TestApp()
app.MainLoop()

Mike Driscoll

unread,
Nov 19, 2009, 12:06:21 PM11/19/09
to wxPython-users
Hi,

On Nov 19, 10:30 am, kakarukeys <kakaruk...@gmail.com> wrote:
> Hi All,
>
> I ran into a strange problem to which I don't have a solution. I am
> writing a systray application which minimizes itself to systray on
> program startup. When binding events to
>
>                 self.Show()
>                 self.Iconize(False)
>
> ONLY IF the event has special ID such as wx.ID_OPEN, then window will
> be activated. If the event has wx.ID_ANY, the window will show and un-
> minimize itself, but will not be on focus. This happens to the first
> time the event is sent, after that, the behavior returns to normal,
> i.e. window will always be on focus.
>
> I'm using
>
> Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit
> (Intel)] on win32
> wxPython 2.8.10.1 (msw-unicode)
>
> Does anyone have an idea how to make the window on focus once the
> event is sent?
>
> Code is as follows, please take any ico file as the py.ico.
> Double-clicking on the icon (event id = wx.ID_ANY will not set the
> window on focus.
> Right-click on the icon and Open (event id = wx.ID_OPEN will set the
> window on focus.
> Although both events are bound to a same method.
>
>

This worked for me on Windows XP Pro, Python 2.5.2, wxPython 2.8.10.1
(unicode). You might try sticking a self.Raise() in the PopupMenu
method.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

kakarukeys

unread,
Nov 19, 2009, 12:41:08 PM11/19/09
to wxPython-users
Million thanks!!! I have been digging through the documentation
searching for something related to 'focus' and 'activate', didn't know
the keyword to use is 'z-order'. I was about to use some dll call and
your ans came.

def OnMenuOpen(self, event):
self.Show()
self.Iconize(False)
self.Raise()
Reply all
Reply to author
Forward
0 new messages