471,330 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

wxpython:how to minimize to taskbar

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?

Aug 28 '07 #1
3 13784
On Aug 28, 9:10 am, Jimmy <mcknight0...@gmail.comwrote:
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

Aug 28 '07 #2
On Aug 28, 9:10 am, Jimmy <mcknight0...@gmail.comwrote:
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

Aug 28 '07 #3
On Aug 28, 9:50 am, "programmer...@gmail.com"
<programmer...@gmail.comwrote:
On Aug 28, 9:10 am, Jimmy <mcknight0...@gmail.comwrote:
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
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

Aug 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Curzio Basso | last post: by
reply views Thread by matthiasjanes | last post: by
reply views Thread by dirvine | last post: by
7 posts views Thread by John Salerno | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.