0

我正在将系统托盘气球通知设为线程。我正在使用 win32api 和 win32gui。我希望每当在代码中调用 balloon_tip(title, msg) 时,应该出现通知气球并且同时继续执行代码。我在代码中多次调用 balloon_tip(title, msg) 来显示通知。但是由于 myThread1 类中的 time.sleep(1) ,控制会等待一秒钟,这会减慢代码的整体流程执行速度。此外,错误 classAtom = RegisterClass(wc) error: (1410, 'RegisterClass', 'Class already exists.')即将到来,但你可以看到,我也在做UnregisterClass(wc.lpszClassName,None)。我认为问题是myThread1 类没有正确线程化

class myThread1(threading.Thread):
    def __init__(self, title, msg):
        threading.Thread.__init__(self)
        self.title=title
        self.msg=msg
        self.daemon = True

    def run(self):  
        message_map = {
                win32con.WM_DESTROY: self.OnDestroy,
        }  
        # Register the Window class.
        iconPathName= D:\\cc.ico
        f.close();

        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) 
        print iconPathName
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        try:
            hicon = LoadImage(hinst,iconPathName, win32con.IMAGE_ICON, 16, 16,icon_flags)
        except:
            hicon = LoadIcon(0, win32con.IDI_APPLICATION)
            logging.debug("Image adding fail")
        flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
        nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "TITLE")
        Shell_NotifyIcon(NIM_ADD, nid)
        Shell_NotifyIcon(NIM_MODIFY, \
                         (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\
                          hicon, "Balloon  tooltip",self.msg,200,self.title))
        # self.show_balloon(title, msg)
        time.sleep(1)
        DestroyWindow(self.hwnd)
        UnregisterClass(wc.lpszClassName,None )
    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):
    thread1 = myThread1(title, msg)
    thread1.start()
4

0 回答 0