您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

wxpython 最小化到托盘与欢迎图片的实现方法

2026/1/11 18:30:47发布21次查看
一直在学习系统托盘的实现,于是自己写了一个简单的系统托盘实例,右键包括演示、最大化、最小化、退出和关于。在python2.6下测试通过。
注意,本节分享的python实例代码,这里是托盘上的图标弹出菜单是覆盖了createpopupmenu。
也可以绑定2个方法,一个wx.evt_taskbar_right_down,方法里面生成menu,然后再来一个wx.evt_menu,定义要处理的事件函数。
还有一个就是wx窗体上的最小化按钮,触发的事件是 wx.evt_iconize,而根本就没有定义什么wx.evt_minimize,但是最大化,有个wx.evt_maximize。
复制代码 代码如下:
#!/usr/bin/python 
# _*_ coding: utf-8 _*_
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='wx.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)
# create a welcome screen 
        screen = wx.image(self.screenim).converttobitmap() 
        wx.splashscreen(screen, wx.splash_centre_on_screen | wx.splash_timeout,1000, none, -1) 
        wx.yield()
self.seticon(wx.icon('wx.ico', wx.bitmap_type_ico)) 
        panel = wx.panel(self, wx.id_any) 
        button = wx.button(panel, wx.id_any, 'hide frame', pos=(60, 60))
sizer = wx.boxsizer() 
        sizer.add(button, 0) 
        panel.setsizer(sizer) 
        self.taskbaricon = taskbaricon(self)
# bind event 
        self.bind(wx.evt_button, self.onhide, button) 
        self.bind(wx.evt_close, self.onclose) 
        self.bind(wx.evt_iconize, self.oniconfiy) # 最小化事件绑定 
    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()
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product