wxpython窗口图标_wxpython 窗口图标和托盘图标

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() 此种方法的png图片只能是32X32的,否则会加载不成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值