wxpython:how to minimize to taskbar

programmer.py at gmail.com programmer.py at gmail.com
Tue Aug 28 10:50:14 EDT 2007


On Aug 28, 9:10 am, Jimmy <mcknight0... at gmail.com> wrote:
> I'm kinda newbie to python and wxPython. Now I'm confronting a thorny
> problem: how can I make  my program minimize to the taskbar
> represented as an ico, and when there is some message from network
> coming, it will pop out?

Warning.  I have not tested this.  I happened to have some old code
that would iconify to the system tray.  Here's what I think you need
to do.  (If it does not work, just yell and I'll try to hack something
together for you.)

Inside the ctor of your mainframe, you'll need to construct a
wxTaskBarIcon (I derive from it).  This is my code that derives from
wxTaskBarIcon.  The comments should give you a good idea of how this
thing works.  I *think* this will shove the icon in the system tray,
even if you're not `iconified` -- so only create it if you want to
show up in the system tray.

## My wxTaskBarIcon derived object...
"""
Taskbar icon.

Not much functionality here, not even a menu.  In the future, this
will be a
good place to allow dclient functionality from the systray.
"""

from wx import TaskBarIcon, EVT_TASKBAR_LEFT_DCLICK

class ddTaskBarIcon(TaskBarIcon):
    def __init__(self, icon, tooltip, frame):
        TaskBarIcon.__init__(self)
        self.SetIcon(icon, tooltip)
        self.frame = frame

        #
        # At the very least, restore the frame if double clicked.  Add
other
        # events later.
        #
        self.Bind(EVT_TASKBAR_LEFT_DCLICK, self.on_left_dclick)

    def on_left_dclick(self, e):
        if self.frame.IsIconized():
            self.frame.Iconize(False)
        if not self.frame.IsShown():
            self.frame.Show(True)
        self.frame.Raise()

    def CreatePopupMenu(self):
        """
        Override with menu functionality, later.
        """
        return None

Next is where I use it in my wxFrame derived object.  This is the code
in my ctor.
    # ddTaskBarIcon is defined above...
    self.trayicon = ddTaskBarIcon(self.frame_icon, "Dap Daemon", self)

    # Handle the window being `iconized` (err minimized)
    self.Bind(wx.EVT_ICONIZE, self.on_iconify)

    # This is the event handler referenced in the ctor above
    def on_iconify(self, e):
        """
        Being minimized, hide self, which removes the program from the
taskbar.
        """
        self.Hide()

So how does this work?  Well, the ddTaskBarIcon (err, i realize this
is a misnomer) is constructed, which puts an icon in the system tray.
The icon has a dbl-click event handler that will `raise` and show the
window if necessary.

The iconify event handler will hide the window if a minimize event
occurs.  This keeps the window from showing up in the windows taskbar.

Thats the magic.  YMMV.

FWIW - the code I reference is over 5 years old and still runs with
wxPython 2.8ish... Kudos to Robin Dunn and crew.  Great job.

jw




More information about the Python-list mailing list