geekdoc-python-zh/docs/pythonlibrary/wxpython-101-creating-taskb...

136 lines
6.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# wxPython 101:创建任务栏图标
> 原文:<https://www.blog.pythonlibrary.org/2011/12/13/wxpython-101-creating-taskbar-icons/>
你有没有想过如何在 Windows 系统托盘中创建那些通常出现在屏幕右下角的小状态图标wxPython 工具包提供了一种非常简单的方法来实现这一点,本文将带您完成这一过程。
## 编写代码
出于我不太明白的原因,我们想要的 wx 组件是 wx.TaskBarIcon我假设之所以这么叫是因为系统托盘是任务栏的一部分或者可能他们没有区分其他操作系统中的任务栏和托盘区域。无论如何首先你需要一个图标来使用。你可以在任何你喜欢的地方得到你的图像。在这个例子中我们将使用一个“信封”图像我使用 wxPython 的 img2py 实用程序将它转换成 Python 代码。当您通过 img2py 运行一个图像时,您将得到如下结果:
```py
#----------------------------------------------------------------------
# This file was generated by img2py.py
#
from wx import ImageFromStream, BitmapFromImage
from wx import EmptyIcon
import cStringIO, zlib
def getData():
return zlib.decompress(
'x\xda\x01\x86\x01y\xfe\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\
\x00\x00 \x08\x06\x00\x00\x00szz\xf4\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\
\x08d\x88\x00\x00\x01=IDATX\x85\xed\x97[\x9a\x83 \x0c\x85Ol\xf7\x1566#\xec\
\xac,\xace\x1e\xe4b \xfa\xa9\xa3\xe5\xc5A\
\x94|\xdd\x81E\x80)y~|(\x17\xd6\xa2\x15"0\x87\xe8`\xc9\xdf\x00@\xd9v\x19\xb2\
\xf0|}-
图像数据都在 **getData** 函数中我将把这个文件命名为email_ico.py”。我们将把它导入到我们的主程序中并调用它的 **getIcon** 方法来获取我们想要使用的图标现在让我们来看看主要的应用程序:
```
import wx
from mail_ico import getIcon
########################################################################
class MailIcon(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
self.tbIcon = getIcon()
self.SetIcon(self.tbIcon, "Test")
# bind some events
self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)
self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnTaskBarLeftClick)
#----------------------------------------------------------------------
def CreatePopupMenu(self, evt=None):
"""
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, "Open Program")
menu.Append(self.TBMENU_CHANGE, "Show all the Items")
menu.AppendSeparator()
menu.Append(self.TBMENU_CLOSE, "Exit Program")
return menu
#----------------------------------------------------------------------
def OnTaskBarActivate(self, evt):
""""""
pass
#----------------------------------------------------------------------
def OnTaskBarClose(self, evt):
"""
Destroy the taskbar icon and frame from the taskbar icon itself
"""
self.frame.Close()
#----------------------------------------------------------------------
def OnTaskBarLeftClick(self, evt):
"""
Create the right-click menu
"""
menu = self.CreatePopupMenu()
self.PopupMenu(menu)
menu.Destroy()
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))
panel = wx.Panel(self)
self.tbIcon = MailIcon(self)
self.Bind(wx.EVT_CLOSE, self.onClose)
#----------------------------------------------------------------------
def onClose(self, evt):
"""
Destroy the taskbar icon and the frame
"""
self.tbIcon.RemoveIcon()
self.tbIcon.Destroy()
self.Destroy()
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()
```py
第一个类基本上是直接从 wxPython 演示中复制出来的并进行了一些简化如果你需要一个更完整的例子你可以去那里看看总之我们在子类 TaskBarIcon 中绑定了两个事件分别允许我们关闭应用程序和显示菜单您还会注意到我们设置了在 **__init__** 中创建的图标只需调用它的 **SetIcon** 方法并为它的工具提示传入一个字符串
close 方法中我们直接调用希望它关闭的框架更好的方法是在这里使用 pubsub如果你想停下来读一下 pubsub我也写了一篇关于它的小文章代码的其余部分非常简单明了
现在我们可以继续到 wx框架子类这里我们基本上只是实例化了我们之前创建的 TaskBarIcon 并将框架绑定到**EVT _ 关闭**你可能会对此感到疑惑 Windows 上使用任务栏图标有一些问题如果我只是告诉框架关闭它关闭得很好但图标仍然存在Python 只是挂在 lala land如果你只允许用户使用任务栏图标的右键菜单来关闭那么你可以添加一个 **RemoveIcon** 方法和一个 selfDestroy()就可以了(出于某种原因RemoveIcon 不足以摆脱 TaskBarIcon所以你也需要告诉它自我销毁)但是如果你允许用户按右上角的小x”,那么你就需要抓住**EVT _ 关闭**并适当地处理它当你抓住这个事件时你不能仅仅调用 **selfClose()** 否则会陷入无限循环这就是我们称 **self 的原因反而破坏()**
包扎
现在您应该能够创建自己的 TaskBarIcon 应用程序了我强烈建议看看 wxPython 演示看看还能做些什么我认为添加一个图标可以为你的应用程序增添一点光彩特别是当你需要让它隐藏一段时间然后在用户的命令下让它弹出来的时候
进一步阅读
```
* [创建一个闪烁的任务栏图标](http://wiki.wxpython.org/FlashingTaskbarIcon)
* 另一个博客的谈到了这个话题
* TaskBarIcon [文档](http://www.wxpython.org/docs/api/wx.TaskBarIcon-class.html)
## 来源
* [TBI_src.tar](https://www.blog.pythonlibrary.org/wp-content/uploads/2011/12/TBI_src.tar)
* [TBI_src.zip](https://www.blog.pythonlibrary.org/wp-content/uploads/2011/12/TBI_src.zip)