import wx
class App(wx.App):
def OnInit(self):
frame = wx.Frame(parent=None, title='Bare')
frame.Show()
icon=wx.EmptyIcon()
icon.LoadFile("loadIcon.ico",wx.BITMAP_TYPE_ICO)
frame.SetIcon(icon)
frame.tbicon=wx.TaskBarIcon()
frame.tbicon.SetIcon(icon,"wxPython Demo")
return True
app = App()
app.MainLoop()
这里有个问题:托盘图标只是显示出来了,并没有响应任何事件。继续研究。
续:找到完整的托盘程序
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='loadIcon.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('Wand.ico', wx.BITMAP_TYPE_ICO))
# The above line is invalid: method become callable after bounding to the instance
self.SetIcon(wx.Icon('loadIcon.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()
另外一种方式:(推荐用第一种方法)
# -*- coding: UTF-8 -*-
import wx
from wx import Frame
class MyFrame(Frame):
def __init__(self, parent, id, title):
Frame.__init__(self, parent, id, title)
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_SIZE, self.OnSize)
icon = wx.EmptyIcon()
icon.CopyFromBitmap(wx.BitmapFromImage(wx.Image(("katon.png"), wx.BITMAP_TYPE_PNG)))
self.SetIcon(icon)
def OnSize(self, event):
size = event.GetSize()
print "size:", size.width, size.height
def OnMove(self, event):
pos = event.GetPosition()
print "pos:", pos.x, pos.y
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, u"测试")
frame.Show(True)
self.SetTopWindow(frame)
return True
def main():
app = MyApp(0)
app.MainLoop()
if __name__ == "__main__":
main()