Setting window to capture keyboard events

Mike,

That's absolutely great - the Raise() method was the one I was looking
for. Have spent so long looking at wxPython docs this week, it somehow
slipped through!

Thanks for the quick response,

Tom

Hi Tom,

Hi,

I'm trying to write some code which will pop up a search box when a
user clicks on the taskbar icon.

However, at the moment, I can't get the app to be 'active', i.e.
accept keyboard input, when the user clicks on the taskbar.

A stripped down version of the code is below, I've looked through all
of the methods from wx.App, wx.Window, wx.TopLevelWindow and wx.Frame,
and can't get anything to bring the frame back to the front.

Does anyone come across this before/ know how to get this behaviour working?

Thanks,

Tom Woolway

Looks like this is the week for taskbar icon questions! I personally
struggled with this and it gets annoying really quickly if you destroy
the frame before the icon. But that's not your question. You should be
able to use the frame's Raise() method. If I minimize the frame, the
Raise() doesn't work. At least, I wasn't able to get it to. You could
change the frame's style so that the user can't minimize it though.

Here's your code with my edits:

<code>

import wx

class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.search = wx.SearchCtrl(self)

        self.task_bar_icon = TaskBarIcon(self)

class TaskBarIcon(wx.TaskBarIcon):
    """
    Adds a Task Bar Icon to the System Tray. Right clicking on this
    will launch a menu, which will allow the user to close the application.
    """
    TBMENU_RESTORE = wx.NewId()
    TBMENU_CLOSE = wx.NewId()
    def __init__(self, frame):
        wx.TaskBarIcon.__init__(self)

        self.frame = frame

        # Bind to events
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
        self.Bind(wx.EVT_MENU, self.OnTaskBarActivate,
id=self.TBMENU_RESTORE)
        self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)

        self.SetIcon(wx.Icon('icon.png', wx.BITMAP_TYPE_PNG), "Icon")

    def CreatePopupMenu(self):
        """
        This method is called by the base class when it needs to popup
        the menu for the default EVT_RIGHT_DOWN event. Just create
        the menu how you want it and return it from this function,
        the base class takes care of the rest.
        """
        menu = wx.Menu()
        menu.Append(self.TBMENU_RESTORE, "Open Program")
        menu.AppendSeparator()
        menu.Append(self.TBMENU_CLOSE, "Exit Program")

        return menu

    def OnTaskBarActivate(self, evt):
        self.frame.Show()
        self.frame.Raise()

    def OnTaskBarClose(self, evt):
        self.frame.Close()

app = wx.App()
MainFrame(None)
app.MainLoop()

</code>

The main thing you were missing was the OnTaskBarActivate() method and
the event to activate that method. I hope that gets you going.

ยทยทยท

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

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org