簡體   English   中英

嘗試在Python中重用Windows Notification類時出錯

[英]Error when trying to reuse Windows Notification class in Python

我正在嘗試使用pywin32模塊在玩游戲時創建更具互動性的體驗,但我遇到了一些問題。 我希望不止一次使用WindowsBalloonTip類,但在嘗試再次使用它時遇到問題。

class WindowsBalloonTip:
    def __init__(self, title, msg):
        message_map = {
                win32con.WM_DESTROY: self.OnDestroy,
        }
        # Register the Window class.
        wc = WNDCLASS()
        hinst = wc.hInstance = GetModuleHandle(None)
        wc.lpszClassName = "PythonTaskbar"
        wc.lpfnWndProc = message_map # could also specify a wndproc.
        classAtom = RegisterClass(wc)
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = CreateWindow( classAtom, "Taskbar", style, \
                0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
                0, 0, hinst, None)
        UpdateWindow(self.hwnd)
        iconPathName = os.path.abspath(os.path.join( sys.path[0], "balloontip.ico" ))
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        try:
           hicon = LoadImage(hinst, iconPathName, \
                    win32con.IMAGE_ICON, 0, 0, icon_flags)
        except:
          hicon = LoadIcon(0, win32con.IDI_APPLICATION)
        flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
        nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "tooltip")
        Shell_NotifyIcon(NIM_ADD, nid)
        Shell_NotifyIcon(NIM_MODIFY, \
                         (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\
                          hicon, "Balloon  tooltip",msg,200,title))
        # self.show_balloon(title, msg)
        DestroyWindow(self.hwnd)
    def OnDestroy(self, hwnd, msg, wparam, lparam):
        nid = (self.hwnd, 0)
        Shell_NotifyIcon(NIM_DELETE, nid)
        PostQuitMessage(0) # Terminate the app.

def balloon_tip(title, msg):
    w=WindowsBalloonTip(title, msg)

要調用WindowsBalloonTip,我使用:

balloon_tip("Fantasy Text", "Welcome to Fantasy Text!")

但是后來當我再次嘗試使用balloon_tip時,我遇到了這個錯誤:

Traceback (most recent call last):
  File "C:\Users\austin\Documents\Programming\Fantasy Text\v1.0.1.0\Fantasy Text.py", line 456, in <module>
    Start()
  File "C:\Users\austin\Documents\Programming\Fantasy Text\v1.0.1.0\Fantasy Text.py", line 89, in Start
    Zombie_Mode()
  File "C:\Users\austin\Documents\Programming\Fantasy Text\v1.0.1.0\Fantasy Text.py", line 224, in Zombie_Mode
    Zombie_Attack()
  File "C:\Users\austin\Documents\Programming\Fantasy Text\v1.0.1.0\Fantasy Text.py", line 240, in Zombie_Attack
    balloon_tip("FT: Zombie", "Zombie did %s" % zombie_damage + ' damage!')
  File "C:\Users\austin\Documents\Programming\Fantasy Text\v1.0.1.0\Fantasy Text.py", line 60, in balloon_tip
    w=WindowsBalloonTip(title, msg)
  File "C:\Users\austin\Documents\Programming\Fantasy Text\v1.0.1.0\Fantasy Text.py", line 32, in __init__
    classAtom = RegisterClass(wc)
pywintypes.error: (1410, 'RegisterClass', 'Class already exists.')

我意識到這個類已經存在於給出的錯誤中,但我該如何解決這個問題呢?

只需稍微重構您的代碼。 創建一個WindowsBalloonTip實例並使用它來顯示所有提示:

class WindowsBalloonTip:
    def __init__(self):
        message_map = {
                win32con.WM_DESTROY: self.OnDestroy,
        }
        # Register the Window class.
        wc = WNDCLASS()
        self.hinst = wc.hInstance = GetModuleHandle(None)
        wc.lpszClassName = "PythonTaskbar"
        wc.lpfnWndProc = message_map # could also specify a wndproc.
        self.classAtom = RegisterClass(wc)

    def ShowWindow(self,title, msg):
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = CreateWindow( self.classAtom, "Taskbar", style, \
                0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
                0, 0, self.hinst, None)
        UpdateWindow(self.hwnd)
        iconPathName = os.path.abspath(os.path.join( sys.path[0], "balloontip.ico" ))
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        try:
           hicon = LoadImage(self.hinst, iconPathName, \
                    win32con.IMAGE_ICON, 0, 0, icon_flags)
        except:
          hicon = LoadIcon(0, win32con.IDI_APPLICATION)
        flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
        nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "tooltip")
        Shell_NotifyIcon(NIM_ADD, nid)
        Shell_NotifyIcon(NIM_MODIFY, \
                         (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\
                          hicon, "Balloon  tooltip",msg,200,title))
        # self.show_balloon(title, msg)
        DestroyWindow(self.hwnd)

    def OnDestroy(self, hwnd, msg, wparam, lparam):
        nid = (self.hwnd, 0)
        Shell_NotifyIcon(NIM_DELETE, nid)
        PostQuitMessage(0) # Terminate the app.

w = WindowsBalloonTip()
w.ShowWindow("Title1","Message One!")
time.sleep(1) # some time later
w.ShowWindow("Title2","Message Two!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM