2011-09-23 10 views
5

के साथ ठीक से करने में असमर्थ है, मैंने हाल ही में wxPython (wxPython 2.9.2.4) के विकास रिलीज में अपग्रेड किया है क्योंकि मुझे अपने आवेदन के भीतर wx.NotificationMessage की कार्यक्षमता की आवश्यकता है। मैं कुछ उपयोगकर्ता घटनाओं पर अधिसूचना बुलबुले बनाने में असफल प्रयास कर रहा हूं क्योंकि मुझे लगता है कि एक संभावित बग हो सकता है। इस तरह की बग सबमिट करने से पहले, मैं आगे बढ़ना चाहता था और मेलिंग सूची के लोगों से पूछना चाहता था कि उन्हें क्या समस्या हो सकती है और उम्मीद है कि मेरे कोड में से एक समाधान मिल जाएगा।wx.NotificationMessage का उपयोग WxPython

यहाँ कोड मैं का इस्तेमाल किया है है: "नमस्ते विश्व"

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

मेरी विंडोज 7 कंप्यूटर पर, कोड एक छोटे सफेद कार्य पट्टी आइकन बनाता है और वाक्यांश के साथ एक पॉपअप पैदा करता है। समस्या? संदेश मेरे आइकन पर नहीं है। एक और आइकन बनाया जा रहा है और संदेश वहां रखा जा रहा है। इस छवि देखें: http://www.pasteall.org/pic/18068 ">

मैं क्या सोचा था कि इस तथ्य यह है कि मैं लाइन 22 पर कोई माता पिता पैरामीटर बीत चुके हैं की वजह से शायद है:

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

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

कहाँ 'स्वयं' कार्य पट्टी आइकन को संदर्भित करता है जब मैं ऐसा, मैं कोई त्रुटि मिलती है:

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

यहाँ क्या मैं इसे करने के लिए बदल रहा है

क्या चल रहा है? अगर मैं उस तर्क को हटा देता हूं, तो मुझे अपना परिणाम नहीं मिलता है, अगर मैं तर्क जोड़ता हूं, तो मुझे एक त्रुटि मिलती है! मुझे 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

ShowBalloon बुलाया TaskBarIcon में एक गैर-दस्तावेजी छिपा विधि है जो केवल 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 पर यह परीक्षण किया है और यह अच्छी तरह से काम करता है।