问题描述
我正在尝试在 wxpython 中创建一个仅使用 TaskBarIcon 而没有框架的应用程序.
I'm trying to create an application in wxpython that only uses a TaskBarIcon and no frames.
这里有一个问题,但那个例子没有'不为我工作;它只是退出,没有错误.
There is a question on this here, but that example doesn't work for me; it just exits with no error.
我在下面编写的代码是我正在使用的代码的简化版:
The code I've written below is a much simplified version of the code I'm working with:
import wx
class Systray_Icon(wx.TaskBarIcon):
def __init__(self):
icon = wx.Icon('yellow.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(icon, "Test")
self.Bind(wx.EVT_MENU, self.Destroy(), id=wx.ID_EXIT)
def CreatePopupMenu(self):
menu = wx.Menu()
menu.Append(wx.ID_EXIT, "Quit")
return menu
app = wx.App()
sysicon = Systray_Icon()
app.MainLoop()
我收到以下错误:
$ python2 systray.py
Traceback (most recent call last):
File "systray.py", line 15, in <module>
sysicon = TaskBarIcon()
File "systray.py", line 6, in __init__
self.SetIcon(icon, "Test")
File "/usr/lib/python2.7/dist-packages/wx-3.0-gtk2/wx/_windows.py", line 2841, in SetIcon
return _windows_.TaskBarIcon_SetIcon(*args, **kwargs)
TypeError: in method 'TaskBarIcon_SetIcon', expected argument 1 of type 'wxPyTaskBarIcon *'
那么,我的问题:
1:为什么 SetIcon 不接受我的课程?我尝试将 SetIcon 调用移动到我链接的问题中的函数,但它仍然不起作用.我可以摆弄它并可能得到一些工作,但我想知道它不起作用的原因.
1: Why won't SetIcon accept my class? I've tried moving the SetIcon call to a function like in the question I linked, but it still doesn't work. I can fiddle around with it and probably get something to work, but I'd like to know the reason it won't work.
2:我链接到的问题运行,但立即退出.那是因为 TaskBarIcon 不会保持 MainLoop() 打开吗?我该怎么办?
2: The question I linked to runs, but exits immediately. Is that because a TaskBarIcon won't hold MainLoop() open? What can I do about this?
推荐答案
这是一个在 Linux 上使用 python 2.7 wxpython 2.8 的工作示例:
Here is a working sample on Linux with python 2.7 wxpython 2.8:
import wx
TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = '/usr/share/pixmaps/thunderbird.xpm'
def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.AppendItem(item)
return item
class TaskBarIcon(wx.TaskBarIcon):
def __init__(self):
wx.TaskBarIcon.__init__(self)
self.set_icon(TRAY_ICON)
self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Say Hello', self.on_hello)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
return menu
def set_icon(self, path):
icon = wx.IconFromBitmap(wx.Bitmap(path))
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print 'Tray icon was left-clicked.'
def on_hello(self, event):
print 'Hello, world!'
def on_exit(self, event):
wx.CallAfter(self.Destroy)
def main():
app = wx.App()
TaskBarIcon()
app.MainLoop()
if __name__ == '__main__':
main()
对于任何仍然感到悲伤的人来说,这是一个基本上使用虚拟框架的版本.
For anyone still getting grief this is a version that essentially uses a dummy frame.
编辑 2019:针对 python3/wxpython 4+ 更新
Edit 2019: Updated for python3/wxpython 4+
import wx
import wx.adv
TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = '/usr/share/pixmaps/python.xpm'
def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.Append(item)
return item
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self,frame):
wx.adv.TaskBarIcon.__init__(self)
self.myapp_frame = frame
self.set_icon(TRAY_ICON)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Say Hello', self.on_hello)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
return menu
def set_icon(self, path):
icon = wx.Icon(wx.Bitmap(path))
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print ('Tray icon was left-clicked.')
def on_hello(self, event):
print ('Hello, world!')
def on_exit(self, event):
self.myapp_frame.Close()
class My_Application(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "", size=(1,1))
panel = wx.Panel(self)
self.myapp = TaskBarIcon(self)
self.Bind(wx.EVT_CLOSE, self.onClose)
#----------------------------------------------------------------------
def onClose(self, evt):
"""
Destroy the taskbar icon and the frame
"""
self.myapp.RemoveIcon()
self.myapp.Destroy()
self.Destroy()
if __name__ == "__main__":
MyApp = wx.App()
My_Application()
MyApp.MainLoop()
这篇关于如何在 wxpython 中创建仅 TaskBarIcon 的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!