wxpython:how to minimize to taskbar

J

Jimmy

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?
 
K

kyosohma

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?

Look at the wxPython demo source code. I found the code I needed in
the main.py file under the DemoTaskBarIcon class, which is a subclass
of wx.TaskBarIcon. You can't view it by running the demo. You have to
open the main.py file itself. On my Windows box, it's found here:
C:\Program Files\wxPython2.8 Docs and Demos\demo

You'll have to call a handler when you receive a message from the
network that basically calls your program's "show" command.

Mike
 
P

programmer.py

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
 
K

kyosohma

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

I've been dinking around with getting one of my programs to minimize
to the system tray for quite a while. I could get the icon, but I
could not get an event to fire when I minimized. Thanks for the code.
Now it works.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top