ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2019-08-29 15:58:13  阅读:582  来源: 互联网

标签:pywin32 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!")

标签:pywin32,python
来源: https://codeday.me/bug/20190829/1761793.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有