システム トレイのバルーン通知をスレッド化しています。win32api と win32gui を使用しています。コードで balloon_tip(title, msg) が呼び出されるたびに、通知バルーンが表示され、同時にコードの実行が続行されるようにしたかったのです。通知を表示するためにコードで balloon_tip(title, msg) を何度も呼び出しています。しかし、myThread1 クラスの time.sleep(1) により、制御は 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()