Ped*_*ira 4 wxpython system-tray python-3.x
我正在使用 WxPython(phoenix 新版本)在程序运行时在托盘栏上显示一个图标,但我会通过一些事件来更改所使用的图标。
我发现了这个很好的例子来开始工作:快速而简单:trayicon with python?
但它没有循环图标的示例。
在导入之后,它有这一行:
TRAY_ICON = 'icon.png'
Run Code Online (Sandbox Code Playgroud)
我尝试将其用作变量,并将以下行添加到事件中(它有一些模拟事件,例如右键单击hello world
TRAY_ICON = 'icon2.png'
Run Code Online (Sandbox Code Playgroud)
但没有成功=//
I only found examples in c, and one in python but using a very complex win32 alternative that I can't figure out
这应该足以解决您的问题。
import wx
import wx.adv
ICON = 'toggle1.png'
ICONS = ["toggle1.png", "toggle2.png"]
X=[1,0]
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
self.toggle = 0
wx.adv.TaskBarIcon.__init__(self)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnToggle)
self.OnSetIcon(ICON)
def CreatePopupMenu(self):
menu = wx.Menu()
togglem = wx.MenuItem(menu, wx.NewId(), 'Toggle Icon')
menu.Bind(wx.EVT_MENU, self.OnToggle, id=togglem.GetId())
menu.Append(togglem)
menu.AppendSeparator()
flashm = wx.MenuItem(menu, wx.NewId(), 'Flash Icon')
menu.Bind(wx.EVT_MENU, self.OnTimer, id=flashm.GetId())
menu.Append(flashm)
menu.AppendSeparator()
quitm = wx.MenuItem(menu, wx.NewId(), 'Quit')
menu.Bind(wx.EVT_MENU, self.OnQuit, id=quitm.GetId())
menu.Append(quitm)
return menu
def OnSetIcon(self, path):
icon = wx.Icon(path)
self.SetIcon(icon, path)
def OnToggle(self, event):
self.toggle=X[self.toggle]
use_icon = ICONS[self.toggle]
self.OnSetIcon(use_icon)
def OnTimer(self,event):
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnInUseTimer)
self.timer.Start(1000)
def OnInUseTimer(self,event):
self.OnToggle(None)
def OnQuit(self, event):
self.RemoveIcon()
wx.CallAfter(self.Destroy)
self.frame.Close()
if __name__ == '__main__':
app = wx.App()
frame=wx.Frame(None)
TaskBarIcon(frame)
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1367 次 |
| 最近记录: |