python - 尝试在 Python 中重用 Windows Notification 类时出错

标签 python pywin32

我正在尝试使用 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!")

关于python - 尝试在 Python 中重用 Windows Notification 类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949186/

相关文章:

python - com_error : (-2147352567, 'Exception occurred.',(0,无,无,无,0,-2147024809),无)

python - pywin32 TOKEN_PRIVILEGES

python - 提取字典中的值

python - 在 Firefox 中继续使用 selenium 之前等待类存在

python - PostGres 为数据帧返回 MemoryError

Python Windows 服务到守护进程服务

python - 蒙特卡洛模拟使用圆估计 pi

python - 如何通过ajax将django模型查询集发送到模板?

python - PyWin32:Windows 经典主题

excel - 使用python对excel中的行进行分组