2011-09-23 47 views
5

我最近升級到wxPython(wxPython 2.9.2.4)的開發版本,因爲我需要在我的應用程序中使用wx.NotificationMessage的功能。由於我認爲可能是一個可能的錯誤,我一直試圖在某些用戶事件上創建通知泡泡失敗。在提交此類錯誤之前,我想繼續向郵件列表中的人員詢問他們認爲可能是問題的原因,並希望從我的代碼中找到解決方案。無法正確使用wx.NotificationMessage和wxPython

這裏是我使用的代碼:「Hello World」的

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電腦,代碼創建了一個白色的小任務欄圖標,並創建短語的彈出。問題?該消息不在我的圖標上。另一個圖標正在創建,並且該消息正被放置在那裏。 看到這個圖片: http://www.pasteall.org/pic/18068" >

我的想法是,這可能是由於這樣的事實,我已經通過了無父參數在第22行:

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

這裏是我把它改爲:

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

在哪裏「自我」是指到任務欄圖標當我這樣做,我得到一個錯誤:

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 *' 

發生了什麼事?如果我刪除了這個參數,我沒有得到結果,如果我添加了參數,我得到一個錯誤!我該如何使用wx.NotificationMessage和wx.TaskBarIcon!

請幫忙!我希望我已經提供了足夠的細節。請評論,如果你需要更多!

+0

您是否找到2.9.2.4的文檔?我要麼失明,要麼運氣不好...... – Fenikso

回答

9

我不會推薦使用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() 
6

有一個在TaskBarIcon無證隱藏的方法叫ShowBalloon了僅針對Windows系統上實現。

the source

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) 

我的wxPython 2.9.4.0測試它在Windows和它工作得很好。