Copy codeThe Code is as follows: import wx
Import images
Class DemoTaskBarIcon (wx. TaskBarIcon ):
TBMENU_RESTORE = wx. NewId ()
TBMENU_CLOSE = wx. NewId ()
TBMENU_CHANGE = wx. NewId ()
TBMENU_REMOVE = wx. NewId ()
Def _ init _ (self, frame ):
Wx. TaskBarIcon. _ init _ (self)
Self. frame = frame
# Set the image
Icon = self. MakeIcon (images. getWXPdemoImage ())
Self. SetIcon (icon, "wxPython Demo ")
Self. imgidx = 1
# Bind some 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. Bind (wx. EVT_MENU, self. OnTaskBarChange, id = self. TBMENU_CHANGE)
Self. Bind (wx. EVT_MENU, self. OnTaskBarRemove, id = self. TBMENU_REMOVE)
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, "Restore wxPython Demo ")
Menu. Append (self. TBMENU_CLOSE, "Close wxPython Demo ")
Menu. AppendSeparator ()
Menu. Append (self. TBMENU_CHANGE, "Change the TB Icon ")
Menu. Append (self. TBMENU_REMOVE, "Remove the TB Icon ")
Return menu
Def MakeIcon (self, img ):
"""
The various platforms have different requirements for
Icon size...
"""
If "wxMSW" in wx. PlatformInfo:
Img = img. Scale (16, 16)
Elif "wxGTK" in wx. PlatformInfo:
Img = img. Scale (22, 22)
# WxMac can be any size upto 128x128, so leave the source img alone ....
Icon = wx. IconFromBitmap (img. ConvertToBitmap ())
Return icon
Def OnTaskBarActivate (self, evt ):
If self. frame. IsIconized ():
Self. frame. Iconize (False)
If not self. frame. IsShown ():
Self. frame. Show (True)
Self. frame. Raise ()
Def OnTaskBarClose (self, evt ):
Self. frame. Close ()
Def OnTaskBarChange (self, evt ):
Names = ["WXPdemo", "Mondrian", "penpencil", "Carrot"]
Name = names [self. imgidx]
GetFunc = getattr (images, "get % sImage" % name)
Self. imgidx + = 1
If self. imgidx> = len (names ):
Self. imgidx = 0
Icon = self. MakeIcon (getFunc ())
Self. SetIcon (icon, "This is a new icon:" + name)
Def OnTaskBarRemove (self, evt ):
Self. RemoveIcon ()
Class MyFrame (wx. Frame ):
Def _ init _ (self ):
Wx. Frame. _ init _ (self, None,-1, "My Frame", size = (300,300 ))
Panel = wx. Panel (self,-1)
Panel. Bind (wx. EVT_MOTION, self. OnMove)
Wx. StaticText (panel,-1, "Pos:", pos = (10, 12 ))
Self. posCtrl = wx. TextCtrl (panel,-1, "", pos = (40, 10 ))
Try:
Self. tbicon = DemoTaskBarIcon (self)
Except t:
Self. tbicon = None
# Wx. CallAfter (self. ShowTip)
# Self. Bind (wx. EVT_CLOSE, self. OnCloseWindow)
# Self. Bind (wx. EVT_ICONIZE, self. OnIconfiy)
Def OnCloseWindow (self, event ):
Self. dying = True
Self. demoPage = None
Self. codePage = None
Self. mainmenu = None
If self. tbicon is not None:
Self. tbicon. Destroy ()
Self. Destroy ()
Def OnIconfiy (self, evt ):
Wx. LogMessage ("OnIconfiy: % s" % evt. Iconized ())
Evt. Skip ()
Def OnMove (self, event ):
Pos = event. GetPosition ()
Self. posCtrl. SetValue ("% s, % s" % (pos. x, pos. y ))
If _ name _ = '_ main __':
App = wx. PySimpleApp ()
Frame = MyFrame ()
Frame. Show (True)
App. MainLoop ()