3

I have a working wxpython app (a simple ssh/rdesktop launcher). I would like to add 3 new features but I don't know where to start with.

1.- When the app starts, or when the users clicks on the standard frame "Minimize" button, I would like the app to minimize to a system-tray icon (I'm running gnome).

2.- When the user left or right clicks the icon, the app should "un-minimize".

3.- When the user presses a hotkey (let's say CTRL+ALT+S), the app sould "un-minimize".

A simple example with an empty window would be appreciated.

PS: Must the icon be loaded from an external ico file? My app is a "single file" .py application ; can I load the resource from the .py itself, from the .ico data in a list/array or similar?

sromero
  • 184
  • 2
  • 16

2 Answers2

1

To get the app to minimize to the system tray, you need to wx.EVT_ICONIZE and in the handler, you just call the frame's Hide() method. You'll need to catch mouse events, like EVT_LEFT_DOWN or EVT_RIGHT_DOWN in the taskbar icon code and Show the app appropriately. There's some sample code in the wxPython demo you can use or this article also has some directions for creating the taskbar icon: http://www.blog.pythonlibrary.org/2008/04/03/reading-openvpn-status-data-with-python/

The hotkey part will need to be registered with the OS itself, and that varies depending on the OS. I have no idea how to get that to work.

To create the icon in Python code, there's a little utility called img2py included with the default wxPython distro: http://www.wxpython.org/docs/api/wx.tools.img2py-module.html Basically it creates the icon in plain text and you can import it and use it that way or you can just copy the code from the created file directly into your main code.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
0

I did something very similar in wxPerl, so here's a brief overview:

You need wx.TaskBarIcon and its events to implement the icon. You can override the window's EVT_CLOSE event to trap the window closure. You should also be able to use EVT_ICONIZE to detect minimizing. Finally, use wxWindow::Show(boolean) to toggle the visibility of the window.

I don't know if it's possible to do a hotkey. The wx Wiki mentions support for global hotkeys, but advises that it works on Windows only. There are also ways to do it using Xlib, but I don't know if that works in wxPython.

It might be easier to add support for a command-line flag that triggers the window of an existing instance, then use the support for custom keyboard shortcuts in GNOME (and Windows) to launch it that way.

Community
  • 1
  • 1
nandhp
  • 4,680
  • 2
  • 30
  • 42