无法在 wxPython 中正确使用 wx.NotificationMessage [英] Unable to use wx.NotificationMessage properly with wxPython

查看:33
本文介绍了无法在 wxPython 中正确使用 wx.NotificationMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到 wxPython (wxPython 2.9.2.4) 的开发版本,因为我需要在我的应用程序中使用 wx.NotificationMessage 的功能.由于我认为可能是一个错误,我一直在尝试在某些用户事件上创建通知气泡未成功.在提交此类错误之前,我想继续询问邮件列表中的人他们认为可能是什么问题,并希望从我的代码中找到解决方案.

I recently upgraded to the development release of wxPython (wxPython 2.9.2.4) since I needed the functionality of wx.NotificationMessage within my application. I have been trying unsuccessfully to create notification bubbles on certain user events due to something I think might be a possible bug. Before submitting such bug, I wanted to go ahead and ask the people of the mailing list what they think might be the problem and hopefully find a solution from within my code.

这是我使用的代码:

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">

On my Windows 7 computer, the code creates a small white task bar icon and creates a popup with the phrase "Hello World!". The problem? The message is not on my icon. Another icon is being created and the message is being placed there. See this image: http://www.pasteall.org/pic/18068">

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

What I thought was that this is probably due to the fact that I have passed no parent parameter on line 22:

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

这是我改成的:

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

self"指的是任务栏图标.当我这样做时,我收到一个错误:

Where 'self' refers to the task bar icon. When I do that, I get an error:

Traceback (most recent call last):
  File "C:Python27	estnotificationmessage.py", line 24, in <module>
    icon = TestTaskBarIcon()
  File "C:Python27	estnotificationmessage.py", line 22, in __init__
    wx.NotificationMessage("", "Hello world!", self).Show()
  File "C:Python27libsite-packageswx-2.9.2-mswwx\_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 *'

这是怎么回事?如果删除该参数,则不会得到结果,如果添加该参数,则会出现错误!我应该如何将 wx.NotificationMessage 与 wx.TaskBarIcon 一起使用!

What's going on? If I remove that argument, I don't get my result, if I add the argument, I get an error! How am I supposed to use wx.NotificationMessage with a wx.TaskBarIcon!

请帮忙!我希望我已经提供了足够的细节.如果您需要更多,请评论!

Please help! I hope I've provided enough details. Please comment if you need more!

推荐答案

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

I would not recommend using 2.9 just yet. I have encountered some strange bugs when trying it out.

您可以在 2.8 中拥有相同的功能.我正在使用我前段时间找到的稍微修改过的代码.

You can have the same functionality in 2.8. I am using somewhat modified code that I have found some time ago.

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()

这篇关于无法在 wxPython 中正确使用 wx.NotificationMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆