2013-06-23 9 views
3

알림 영역에 아래의 코드를 사용하여 트레이 팁 (BaloonTips)을 만드는 경우 하나의 메시지 만 허용 한 다음 멈추고 오류가 발생합니다.여러 WindowsBaloonTip/TrayTip 알림?

이 코드는 here에서 찍은 :

# -- coding: utf-8 -- 

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

class WindowsBalloonTip: 
    def __init__(self, title, msg): 
     message_map = { 
       win32con.WM_DESTROY: self.OnDestroy, 
     } 
     # 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") 
     Shell_NotifyIcon(NIM_ADD, nid) 
     Shell_NotifyIcon(NIM_MODIFY, \ 
         (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\ 
          hicon, "Balloon tooltip",msg,200,title)) 
     # self.show_balloon(title, msg) 
     time.sleep(10) 
     DestroyWindow(self.hwnd) 
    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): 
    w=WindowsBalloonTip(title, msg) 

답변

7

당신이 시도하고 다음과 같은 오류 얻을 것이다 WindowsBalloonTip 클래스에 여러 balloon_tip 년대를 보낼 수 있다면 당신이 볼 수 있듯이 :

File "C:/WindowsBalloonTip.py", line 20, in __init__ 
    classAtom = RegisterClass(wc) 
error: (1410, 'RegisterClass', 'Class already exists.') 

상대를 RegisterClass에 대한 Microsoft의 설명서에서 다음을 사용하여 클래스 등록을 취소해야합니다. UnregisterClass(lpClassName, hInstance)

http://msdn.microsoft.com/en-us/library/ms644899

우리는 추가하여이 작업을 수행 할 수 있습니다

: classAtom = UnregisterClass(classAtom, hinst)를하지만 창이 여기에 예를 들어 파괴 된 후에는 추가해야합니다

# -- coding: utf-8 -- 

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

# Class 
class WindowsBalloonTip: 
    def __init__(self, title, msg): 
     message_map = { win32con.WM_DESTROY: self.OnDestroy,} 

     # 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) 

     # Icons managment 
     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, 200, title)) 
     # self.show_balloon(title, msg) 
     time.sleep(5) 

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

# Function 
def balloon_tip(title, msg): 
    w=WindowsBalloonTip(title, msg) 

# Main 
if __name__ == '__main__': 
    # Example 
    balloon_tip('Lorem Ipsum', 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...') 
    balloon_tip('Example two', 'There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain...') 

이 아이콘이 특정 운영 체제에 문제가 있습니다 고정 된 풀 버전입니다 로딩 자신의 사용자 정의 아이콘 다음이 라인을 변경하려면 너무 :

: 뭔가에

iconPathName = os.path.abspath(os.path.join(sys.path[0], 'balloontip.ico')) 

+0

새 스레드에서 수업 등록을 취소 할 때까지 [나]도 작동합니다 ... http://stackoverflow.com/questions/24091211/refresh-windows-balloon-tip-from-python-thread .. . 이견있는 사람? [동료를 대신하여 논평하기] – humbolight

+1

Windows 10 용 작업 센터에서 메시지가 지속되도록하는 방법이 있습니까? –