首页 > 解决方案 > wxpython绑定windows toast通知处理程序的问题

问题描述

我正在尝试制作一个 Windows 10 toast 通知,如果它被点击,它将运行代码,但我的代码只显示通知并在我点击它时给我一个错误

import os
import wx
import wx.adv


class MyApp(wx.App):
    def OnInit(self):
        sTitle = 'test'
        sMsg = 'test'
        nmsg = wx.adv.NotificationMessage(title=sTitle, message=sMsg)
        nmsg.SetFlags(wx.ICON_INFORMATION)
        nmsg.Show(timeout=wx.adv.NotificationMessage.Timeout_Auto)
        self.Bind(wx.EVT_NOTIFICATION_MESSAGE_CLICK, self.notifclicked)
        return True
    def _notifclicked(self, evt):
        print("notification has been clicked")
app = MyApp()
app.MainLoop()

错误代码:AttributeError:模块'wx'没有属性'EVT_NOTIFICATION_MESSAGE_CLICK'

标签: pythonnotificationswxpython

解决方案


不想肯定地说那NotificationMessage是未完成的事情,我将建议它。

我怀疑它基于notify并且notify2声称支持action回调但不支持。
他们应该依赖 Dbus 发送东西,但它看起来不像它,或者你必须跳过箍来实现它。(例如 Dbus MainLoop 设置)

我决定稍微修改你的代码,只是为了展示如何添加action按钮,尽管它们只是吸引眼球,并展示我认为事件回调应该如何工作,如果它成为现实的话。

当然,如果它目前确实有效并且我还没有弄清楚如何去做,我会很高兴地吃掉这些话。我专门在 Linux 上编写代码,因此需要注意。

import wx
import wx.adv

Act_Id_1 = wx.NewIdRef()
Act_Id_2 = wx.NewIdRef()

class MyFrame(wx.Frame):
    def __init__(self, parent=None, id=wx.ID_ANY, title="", size=(360,100)):
        super(MyFrame, self).__init__(parent, id, title, size)
        self.m_no = 0
        self.panel = wx.Panel(self)
        self.Mbutton = wx.Button(self.panel, wx.ID_ANY, label="Fire off a message", pos=(10,10))
        self.Bind(wx.EVT_BUTTON, self.OnFire, self.Mbutton)
        #self.Bind(wx.adv.EVT_NOTIFICATION_MESSAGE_CLICK, self._notifclicked)
        #self.Bind(wx.adv.EVT_NOTIFICATION_MESSAGE_DISMISSED, self._notifdismissed)
        #self.Bind(wx.adv.EVT_NOTIFICATION_MESSAGE_ACTION, self._notifaction, id=Act_Id_1)
        self.Show()

    def OnFire(self, event):
        self.m_no +=1
        sTitle = 'Test heading'
        sMsg = 'Test message No '
        nmsg = wx.adv.NotificationMessage(title=sTitle, message=sMsg+str(self.m_no))
        nmsg.SetFlags(wx.ICON_INFORMATION)
        nmsg.AddAction(Act_Id_1, "Cancel")
        nmsg.AddAction(Act_Id_2, "Hold")
        nmsg.Show(timeout=10)

    def _notifclicked(self, event):
        print("notification has been clicked")
    def _notifdismissed(self, event):
        print("notification dismissed")
    def _notifaction(self, event):
        print("Action")
app = wx.App()
frame = MyFrame()
app.MainLoop()

推荐阅读