0

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()
Harish Barvekar
  • 83
  • 2
  • 2
  • 7
  • Surely you need a message loop. You don't unregister the class if exceptions are raised. You make no attempt at all to handle errors. Win32 functions don't raise exceptions when they fail. Check return values. There's no point in anyone looking at this until you've added error checking. – David Heffernan Oct 02 '13 at 07:28
  • @David Heffernan can you elaborate what i have to make changes in the code – Harish Barvekar Oct 02 '13 at 18:37
  • Well, I think you need a message pump, and some error checking. Are you familiar with error handling in Win32? – David Heffernan Oct 02 '13 at 18:38
  • @David Heffernan No, not much about error handling. Can you suggest that code is correctly threaded or not. – Harish Barvekar Oct 03 '13 at 03:41
  • check the return value of each Win32 call. Read the docs for each function to learn how to interpret it. – David Heffernan Oct 03 '13 at 06:03

0 Answers0