从Python线程刷新Windows Balloon提示

时间:2014-06-06 21:55:22

标签: python multithreading pywin32 system-tray balloon-tip

每当用户收到新消息时,我都会在系统托盘中使用模块进行气球提示通知。 (使用Python 3.3和Kivy 1.8。我正在部署到Windows 7。)

我认为原始代码来自this github

这是我修改过的模块::

from win32api import *
from win32gui import *
import win32con
import sys, os
import struct
import threading
import time

print ("Importing pypops")

class WindowsBalloonTip:
    def __init__(self, title, msg):

        message_map = {
                win32con.WM_DESTROY: self.OnDestroy,
                # win32con.WM_CLOSE: self.onClose
        }
        # 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")

        #Notify
        Shell_NotifyIcon(NIM_ADD, nid)
        Shell_NotifyIcon(NIM_MODIFY, \
                         (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\
                          hicon, "Balloon  tooltip",msg,400,title))

        ### from original code
        # time.sleep(10) #cannot use this. it kills the whole app
        # DestroyWindow(self.hwnd) 
        # classAtom = UnregisterClass(classAtom, hinst) 

    def onClose(self, hwnd): #to be called from thread as a method of the class instance
        DestroyWindow(hwnd)
        classAtom = UnregisterClass(classAtom, hinst)
        return True

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

我正在实例化线程中的WindowsBalloonTip类,当且仅当有新消息进入时。在实例化类之后,如果后续线程检测到另一个新消息,我试图调用{{ 1}}作为pypops类的一种方法,并获得

  

pywintypes.error:(1400,' DestroyWindow','无效的窗口句柄。')。

我从其他来源得知,这是因为必须使用创建窗口的线程来销毁它。这是怎么回事?

我的代码中注释掉的onClose解决方案至少在以下原因下是不可行的:

  1. 我需要窗口保持活着,直到a)用户点击或b)新消息进入
  2. time.sleep杀死我的整个应用,阻止用户输入,重绘等
  3. 我研究过: Handling invalid window handle 和许多其他人。

    如何从一个线程中动态销毁窗口并取消注册该类,或更新窗口time.sleep()

    当我尝试将Text作为顺序DestroyWindow步骤时,它可以正常工作。记录--init--会产生一个整数。但是,当我尝试调用'hwnd'时,我收到了1400错误,即使在这种情况下记录onClose会产生与其他'hwnd'完全相同的值:换句话说,我传递给'hwnd'的参数是DestroyWindow阶段DestroyWindow发生的相同值,还是我稍后调用的方法的一部分。什么是后一种情况下的错误?

    我对Python非常熟悉,并且对Kivy越来越好。但是,这是我第一次涉足任何windows gui / api兼容性。

    感谢您的任何答案,建议,批评,线索

    "信息?导致,是的,确定。我呃,请在犯罪实验室与男孩们一起检查。他们呃,呃,还有四个侦探正在研究这个案子。他们让我们轮班工作。"

0 个答案:

没有答案