python中wx将图标显示在右下角的脚本代码

 更新时间:2013年03月08日 12:11:08   作者:  
python中wx将图标显示在右下脚的代码,此程序摘自wxdemo,不够完善,只供参考用

复制代码 代码如下:

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 the
        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", "Pencil", "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:
            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()

相关文章

  • 已安装tensorflow-gpu,但keras无法使用GPU加速的解决

    已安装tensorflow-gpu,但keras无法使用GPU加速的解决

    今天小编就为大家分享一篇已安装tensorflow-gpu,但keras无法使用GPU加速的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • python中的列表推导浅析

    python中的列表推导浅析

    这篇文章主要介绍了python中的列表推导,需要的朋友可以参考下
    2014-04-04
  • Python中Numpy的深拷贝和浅拷贝

    Python中Numpy的深拷贝和浅拷贝

    这篇文章主要介绍了Python中Numpy的深拷贝和浅拷贝,通过讲解Python中对Numpy数组操作的浅拷贝和深拷贝的概念和背后的原理展开全文,需要的小伙伴可以参考一下
    2022-05-05
  • Python学习笔记之错误和异常及访问错误消息详解

    Python学习笔记之错误和异常及访问错误消息详解

    这篇文章主要介绍了Python学习笔记之错误和异常及访问错误消息,结合实例形式分析了Python错误和异常及访问错误消息try...except语句相关使用技巧,需要的朋友可以参考下
    2019-08-08
  • python 列表的查询操作和切片

    python 列表的查询操作和切片

    这篇文章主要介绍了python 列表的查询操作和切片,列表是python内置的数据结构,相当于数组,列表中所有数据都是按顺序有序排列,列表属于序列类型,接下来一起学习下面的文章内容吧
    2022-01-01
  • 一文秒懂pandas中iloc()函数

    一文秒懂pandas中iloc()函数

    iloc[]函数属于pandas库全称为index location,即对数据进行位置索引,从而在数据表中提取出相应的数据,本文通过实例代码介绍pandas中iloc()函数,感兴趣的朋友一起看看吧
    2023-04-04
  • 可能是史上最细的python中import详解

    可能是史上最细的python中import详解

    import在python中的意思是用来调用模块的,下面这篇文章主要给大家介绍了关于python中import详解的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-02-02
  • 如何在Python中妥善使用进度条详解

    如何在Python中妥善使用进度条详解

    python的进度条有很多第三方库,有些做的比较炫酷,下面这篇文章主要给大家介绍了关于如何在Python中妥善使用进度条的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-04-04
  • 一文带你掌握Python中enumerate函数和for循环的对比

    一文带你掌握Python中enumerate函数和for循环的对比

    在Python编程中,循环是一项常见的任务,而for循环是最常见的一种,然而,Python提供了enumerate函数,它允许在迭代过程中访问元素的同时获得它们的索引,下面我们就来学习一下二者的区别吧
    2023-11-11
  • 用于统计项目中代码总行数的Python脚本分享

    用于统计项目中代码总行数的Python脚本分享

    这篇文章主要介绍了用于统计项目中代码总行数的Python脚本分享,本文直接给出实现代码,需要的朋友可以参考下
    2015-04-04

最新评论