无法将wx.NotificationMessage正确用于wxPython

无法将wx.NotificationMessage正确用于wxPython,python,notifications,wxpython,message,wxwidgets,Python,Notifications,Wxpython,Message,Wxwidgets,我最近升级到了wxPython的开发版本(wxpython2.9.2.4),因为我的应用程序中需要wx.NotificationMessage的功能。我一直试图在某些用户事件上创建通知气泡,但没有成功,因为我认为这可能是一个bug。在提交这样的bug之前,我想继续问邮件列表中的人他们认为可能存在什么问题,并希望从我的代码中找到解决方案 以下是我使用的代码: import wx, sys app = wx.PySimpleApp() class TestTaskBarIcon(wx.TaskB

我最近升级到了wxPython的开发版本(wxpython2.9.2.4),因为我的应用程序中需要wx.NotificationMessage的功能。我一直试图在某些用户事件上创建通知气泡,但没有成功,因为我认为这可能是一个bug。在提交这样的bug之前,我想继续问邮件列表中的人他们认为可能存在什么问题,并希望从我的代码中找到解决方案

以下是我使用的代码:

import wx, sys

app = wx.PySimpleApp()

class TestTaskBarIcon(wx.TaskBarIcon):

    def __init__(self):
        wx.TaskBarIcon.__init__(self)
        # create a test icon
        bmp = wx.EmptyBitmap(16, 16)
        dc = wx.MemoryDC(bmp)
        dc.SetBrush(wx.RED_BRUSH)
        dc.Clear()
        dc.SelectObject(wx.NullBitmap)

        testicon = wx.EmptyIcon()
        testicon.CopyFromBitmap(bmp)

        self.SetIcon(testicon)
        self.Bind(wx.EVT_TASKBAR_LEFT_UP, lambda e: (self.RemoveIcon(),sys.exit()))

        wx.NotificationMessage("", "Hello world!").Show()

icon = TestTaskBarIcon()
app.MainLoop()
在我的Windows 7计算机上,代码创建了一个白色的小任务栏图标,并创建了一个带有短语“Hello World!”的弹出窗口。问题出在哪里?该消息不在我的图标上。正在创建另一个图标,并将消息放置在那里。 请参见此图: http://www.pasteall.org/pic/18068“>

我认为这可能是因为我没有在第22行传递任何父参数:

wx.NotificationMessage("", "Hello world!").Show()
以下是我将其更改为:

wx.NotificationMessage("", "Hello world!", self).Show()
其中“self”指的是任务栏图标。当我这样做时,我得到一个错误:

Traceback (most recent call last):
  File "C:\Python27\testnotificationmessage.py", line 24, in <module>
    icon = TestTaskBarIcon()
  File "C:\Python27\testnotificationmessage.py", line 22, in __init__
    wx.NotificationMessage("", "Hello world!", self).Show()
  File "C:\Python27\lib\site-packages\wx-2.9.2-msw\wx\_misc.py", line 1213, in __init__
    _misc_.NotificationMessage_swiginit(self,_misc_.new_NotificationMessage(*args))
TypeError: in method 'new_NotificationMessage', expected argument 3 of type 'wxWindow *'
回溯(最近一次呼叫最后一次):
文件“C:\Python27\testnotificationmessage.py”,第24行,在
icon=TestTaskBarIcon()
文件“C:\Python27\testnotificationmessage.py”,第22行,在\uuu init中__
NotificationMessage(“,“你好,世界!”,self.Show())
文件“C:\Python27\lib\site packages\wx-2.9.2-msw\wx\\u misc.py”,第1213行,在uu init中__
_杂项通知消息交换(self,杂项通知消息新建通知消息(*args))
TypeError:在方法“new_NotificationMessage”中,应为“wxWindow*”类型的参数3
发生了什么事?如果我删除了那个参数,我就不会得到结果,如果我添加了这个参数,我就会得到一个错误!我应该如何使用wx.NotificationMessage和wx.TaskBarIcon


请帮助!我希望我已经提供了足够的详细信息。如果您需要更多信息,请发表评论!

我现在还不建议使用2.9。我在试用时遇到了一些奇怪的错误

您可以在2.8中使用相同的功能。我正在使用我不久前发现的经过一些修改的代码

import wx, sys

try:
    import win32gui #, win32con
    WIN32 = True
except:
    WIN32 = False

class BalloonTaskBarIcon(wx.TaskBarIcon):
    """
    Base Taskbar Icon Class
    """
    def __init__(self):
        wx.TaskBarIcon.__init__(self)
        self.icon = None
        self.tooltip = ""

    def ShowBalloon(self, title, text, msec = 0, flags = 0):
        """
        Show Balloon tooltip
         @param title - Title for balloon tooltip
         @param msg   - Balloon tooltip text
         @param msec  - Timeout for balloon tooltip, in milliseconds
         @param flags -  one of wx.ICON_INFORMATION, wx.ICON_WARNING, wx.ICON_ERROR
        """
        if WIN32 and self.IsIconInstalled():
            try:
                self.__SetBalloonTip(self.icon.GetHandle(), title, text, msec, flags)
            except Exception:
                pass # print(e) Silent error

    def __SetBalloonTip(self, hicon, title, msg, msec, flags):

        # translate flags
        infoFlags = 0

        if flags & wx.ICON_INFORMATION:
            infoFlags |= win32gui.NIIF_INFO
        elif flags & wx.ICON_WARNING:
            infoFlags |= win32gui.NIIF_WARNING
        elif flags & wx.ICON_ERROR:
            infoFlags |= win32gui.NIIF_ERROR

        # Show balloon
        lpdata = (self.__GetIconHandle(),   # hWnd
                  99,                       # ID
                  win32gui.NIF_MESSAGE|win32gui.NIF_INFO|win32gui.NIF_ICON, # flags: Combination of NIF_* flags
                  0,                        # CallbackMessage: Message id to be pass to hWnd when processing messages
                  hicon,                    # hIcon: Handle to the icon to be displayed
                  '',                       # Tip: Tooltip text
                  msg,                      # Info: Balloon tooltip text
                  msec,                     # Timeout: Timeout for balloon tooltip, in milliseconds
                  title,                    # InfoTitle: Title for balloon tooltip
                  infoFlags                 # InfoFlags: Combination of NIIF_* flags
                  )
        win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, lpdata)

        self.SetIcon(self.icon, self.tooltip)   # Hack: because we have no access to the real CallbackMessage value

    def __GetIconHandle(self):
        """
        Find the icon window.
        This is ugly but for now there is no way to find this window directly from wx
        """
        if not hasattr(self, "_chwnd"):
            try:
                for handle in wx.GetTopLevelWindows():
                    if handle.GetWindowStyle():
                        continue
                    handle = handle.GetHandle()
                    if len(win32gui.GetWindowText(handle)) == 0:
                        self._chwnd = handle
                        break
                if not hasattr(self, "_chwnd"):
                    raise Exception
            except:
                raise Exception, "Icon window not found"
        return self._chwnd

    def SetIcon(self, icon, tooltip = ""):
        self.icon = icon
        self.tooltip = tooltip
        wx.TaskBarIcon.SetIcon(self, icon, tooltip)

    def RemoveIcon(self):
        self.icon = None
        self.tooltip = ""
        wx.TaskBarIcon.RemoveIcon(self)

# ===================================================================
app = wx.PySimpleApp()

class TestTaskBarIcon(BalloonTaskBarIcon):

    def __init__(self):
        wx.TaskBarIcon.__init__(self)
        # create a test icon
        bmp = wx.EmptyBitmap(16, 16)
        dc = wx.MemoryDC(bmp)
        dc.SetBrush(wx.RED_BRUSH)
        dc.Clear()
        dc.SelectObject(wx.NullBitmap)

        testicon = wx.EmptyIcon()
        testicon.CopyFromBitmap(bmp)

        self.SetIcon(testicon)
        self.Bind(wx.EVT_TASKBAR_LEFT_UP, lambda e: (self.RemoveIcon(),sys.exit()))

        self.ShowBalloon("", "Hello world!")

icon = TestTaskBarIcon()
app.MainLoop()

TaskBarIcon
中有一个名为
ShowBalloon
的未记录隐藏方法,该方法仅在Windows中实现

发件人:


我用wxPython 2.9.4.0在Windows上测试了它,效果很好。

你找到2.9.2.4的文档了吗?我不是瞎子就是运气不好。。。
def ShowBalloon(*args, **kwargs):
    """
    ShowBalloon(self, String title, String text, unsigned int msec=0, int flags=0) -> bool

    Show a balloon notification (the icon must have been already
    initialized using SetIcon).  Only implemented for Windows.

    title and text are limited to 63 and 255 characters respectively, msec
    is the timeout, in milliseconds, before the balloon disappears (will
    be clamped down to the allowed 10-30s range by Windows if it's outside
    it) and flags can include wxICON_ERROR/INFO/WARNING to show a
    corresponding icon

    Returns True if balloon was shown, False on error (incorrect parameters
    or function unsupported by OS)

    """
    return _windows_.TaskBarIcon_ShowBalloon(*args, **kwargs)