Custom Controls → TaskBarIcon
Overview = """\
A taskbar icon is an icon that appears in the 'system tray' and responds
to mouse clicks, optionally with a tooltip above it to help provide
information.
Pay More Attention To Destroy-Operation Of TaskBarIcon!
"""
import wx
class TaskBarIcon(wx.TaskBarIcon):
ID_Hello = wx.NewId()
def __init__(self, frame):
wx.TaskBarIcon.__init__(self)
self.frame = frame
self.SetIcon(wx.Icon(name='wxWidgets.ico', type=wx.BITMAP_TYPE_ICO), 'TaskBarIcon!')
self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick)
self.Bind(wx.EVT_MENU, self.OnHello, id=self.ID_Hello)
def OnTaskBarLeftDClick(self, event):
if self.frame.IsIconized():
self.frame.Iconize(False)
if not self.frame.IsShown():
self.frame.Show(True)
self.frame.Raise()
def OnHello(self, event):
wx.MessageBox('Hello From TaskBarIcon!', 'Prompt')
# override
def CreatePopupMenu(self):
menu = wx.Menu()
menu.Append(self.ID_Hello, 'Hello')
return menu
class Frame(wx.Frame):
def __init__(
self, parent=None, id=wx.ID_ANY, title='TaskBarIcon', pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE
):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
# wx.Frame.SetIcon(wx.Icon('wxWidgets.ico', wx.BITMAP_TYPE_ICO))
# The above line is invalid: method become callable after bounding to the instance
self.SetIcon(wx.Icon('wxWidgets.ico', wx.BITMAP_TYPE_ICO))
panel = wx.Panel(self, wx.ID_ANY)
button = wx.Button(panel, wx.ID_ANY, 'Hide Frame', pos=(60, 60))
self.Bind(wx.EVT_BUTTON, self.OnHide, button)
# button = wx.Button(panel, wx.ID_ANY, 'Close', pos=(60, 100))
# self.Bind(wx.EVT_BUTTON, self.OnClose, button)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Bind(wx.EVT_ICONIZE, self.OnIconfiy) # What is the meaning?
self.taskBarIcon = TaskBarIcon(self)
sizer = wx.BoxSizer()
sizer.Add(button, 0)
panel.SetSizer(sizer)
def OnHide(self, event):
self.Hide()
def OnIconfiy(self, event):
wx.MessageBox('Frame has been iconized!', 'Prompt')
event.Skip()
def OnClose(self, event):
self.taskBarIcon.Destroy()
self.Destroy()
def TestFrame():
app = wx.PySimpleApp()
frame = Frame(size=(640, 480))
frame.Centre()
frame.Show()
app.MainLoop()
if __name__ == '__main__':
TestFrame()