I am making the system tray balloon notification as threaded. I am using win32api and win32gui. I wanted that whenever balloon_tip(title, msg) is called in the code, notification balloon should appear and simultaneously the execution of code goes on.I am calling the balloon_tip(title, msg) many times in code to show notification. But due to time.sleep(1) in myThread1 class, control is waiting for one second which slows the overall process execution of code. Also, error that classAtom = RegisterClass(wc) error: (1410, 'RegisterClass', 'Class already exists.') is coming but you can see, i am also doing UnregisterClass(wc.lpszClassName,None). The problem i think is myThread1 class is not properly threaded.
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()