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

时间:2022-10-27 02:26:53 


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()

标签:图标,右下角
0
投稿

猜你喜欢

  • python函数默认参数使用避坑指南

    2023-06-19 13:37:50
  • 使用python3+xlrd解析Excel的实例

    2021-05-09 23:37:33
  • 详细分析Python collections工具库

    2022-06-28 01:18:57
  • Python使用requests xpath 并开启多线程爬取西刺代理ip实例

    2023-05-01 23:36:51
  • Python实现的爬取百度贴吧图片功能完整示例

    2021-06-30 19:22:13
  • SQLServer行列互转实现思路(聚合函数)

    2024-01-15 15:13:33
  • python实现简单石头剪刀布游戏

    2023-03-08 03:23:44
  • Python实现爬取逐浪小说的方法

    2022-05-26 22:31:29
  • python ChainMap的使用和说明详解

    2022-03-03 08:22:30
  • 解决python将xml格式文件转换成txt文件的问题(xml.etree方法)

    2021-10-21 02:51:13
  • javascript一个无懈可击的实例化XMLHttpRequest的方法

    2024-04-16 09:49:19
  • python3安装speech语音模块的方法

    2023-03-24 12:09:32
  • python通过nmap扫描在线设备并尝试AAA登录(实例代码)

    2021-08-06 23:23:42
  • asp详解session的用法

    2007-09-07 10:22:00
  • python实现批量获取指定文件夹下的所有文件的厂商信息

    2021-12-14 20:42:27
  • MYSQL拒绝访问报错not allowed to connect

    2024-01-16 02:20:21
  • 代码详解Python的函数基础(2)

    2023-08-11 17:59:40
  • OpenCV如何去除图片中的阴影的实现

    2023-07-08 22:49:29
  • Python使用Tkinter实现滚动抽奖器效果

    2023-04-07 03:05:45
  • Django contrib auth authenticate函数源码解析

    2022-04-27 01:49:23
  • asp之家 网络编程 m.aspxhome.com