wxPython 系统托盘图标
我正在尝试在我的应用程序的系统托盘中实现一个图标,但是我有两个问题。
首先,虽然我使用的图标是具有透明背景的.png,但该图标具有丑陋的白色背景。
其次,该图标有一个右键菜单,其中包含“显示”和“关闭”选项,但由于未知原因,它们旁边都显示“Ctrl - Q”。我不仅没有指定这一点,而且热键组合也没有任何作用。
这是我正在使用的代码。它几乎直接从文档中提取:
class SysTray(wx.TaskBarIcon):
def __init__(self, parent, icon, text):
wx.TaskBarIcon.__init__(self)
self.parentApp = parent
self.SetIcon(icon, text)
self.CreateMenu()
def CreateMenu(self):
self.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.ShowMenu)
self.menu=wx.Menu()
self.menu.Append(wx.ID_OPEN, "Show")
self.menu.Append(wx.ID_EXIT, "Close")
def ShowMenu(self,event):
self.PopupMenu(self.menu)
这是使用以下方法实现的:
self.trayicon = SysTray(self, wx.Icon(TRAY_ICON, wx.BITMAP_TYPE_PNG), TRAY_TOOLTIP)
self.trayicon.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
self.trayicon.Bind(wx.EVT_MENU, self.OnShow, id=wx.ID_OPEN)
I'm trying to implement a icon in the system tray for my application however I have two problems.
Firstly, although the icon I am using is a .png with a transparent background the icon has an ugly white background.
Second, the Icon has a right click menu with the options "Show" and "Close" however for unknown reasons both say "Ctrl - Q" next to them. Not only did I not specify this, but the hotkey combo does nothing.
Here is the code I am using. It's almost directly lifted from the documentation:
class SysTray(wx.TaskBarIcon):
def __init__(self, parent, icon, text):
wx.TaskBarIcon.__init__(self)
self.parentApp = parent
self.SetIcon(icon, text)
self.CreateMenu()
def CreateMenu(self):
self.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.ShowMenu)
self.menu=wx.Menu()
self.menu.Append(wx.ID_OPEN, "Show")
self.menu.Append(wx.ID_EXIT, "Close")
def ShowMenu(self,event):
self.PopupMenu(self.menu)
Which is implemented using:
self.trayicon = SysTray(self, wx.Icon(TRAY_ICON, wx.BITMAP_TYPE_PNG), TRAY_TOOLTIP)
self.trayicon.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
self.trayicon.Bind(wx.EVT_MENU, self.OnShow, id=wx.ID_OPEN)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
wxPython 使用称为 AcceleratorTable 的东西来跟踪“热键”或“快捷方式”或任何你想称呼它们的东西。与定义和设置 sizer 的方式类似,您可以定义和设置 AcceleratorTable 并且 wxPython 将使用它。更多信息请参见http://www.blog。 pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/
另外,为了获得 Ctrl-Q 或 Ctrl-O您需要在文本中指定热键或将它们添加到加速器表中。如果您将格式正确的文本添加到菜单项,wxPython 应该识别“这是一个热键”并自动将其添加到加速表中。
wxPython uses something called an AcceleratorTable for keeping track of "hotkeys" or "shortcuts" or whatever you'd like to call them. Similar to the way you can define and set a sizer, you can define and set an AcceleratorTable and wxPython will use it. More on that here http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/
Also, in order to get the Ctrl-Q or Ctrl-O hotkeys you need to either specify them in the text or add them to the accelerator table. If you add the properly formatted text to the menu items, wxPython should recognize "this is a hotkey" and add it to the accelerator table for you automatically.