1

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

我相信原始代码来自这个 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在线程中实例化类,当且仅当有新消息进入时。类实例化后,如果后续线程检测到另一个新消息,我试图onClose作为 pypops 类的方法调用,并得到

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

我从其他来源知道这是因为必须使用创建窗口的线程来销毁它。解决方法是什么?

time.sleep至少由于以下原因,在我的代码中注释掉的解决方案是不可行的:

  1. 我需要窗口保持活动状态,直到 a) 用户单击或 b) 有新消息进入
  2. time.sleep()杀死我的整个应用程序,阻止用户输入,重绘等

我研究过: 处理无效的窗口句柄 和许多其他的。

如何销毁窗口并从线程中动态注销类或更新窗口Text

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

我对 Python 非常熟悉,并且对 Kivy 越来越熟悉。然而,这是我第一次尝试任何 windows gui/api 互兼容。

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

“线索?线索,是的。我会呃,和犯罪实验室的男孩们核实一下。他们呃,呃,还有四个侦探在处理这个案子。他们让我们轮班工作。”

4

0 回答 0