SoFunction
Updated on 2024-11-07

Python implementation of desktop tray bubble alerts

In this article, we share the example of python to achieve the specific code of the desktop tray bubble tips, for your reference, the specific content is as follows

# -*- encoding:utf-8 -*- 
##############################
#
# Program name: python desktop tray bubbles
# filename:
# Function : Realize desktop tray bubble alert function
# modify:by adengou 2016.1.4
# program:python3.4.4
# Applicable : WindowsXP - Windows10
#
##############################
import sys 
import os 
import struct 
import time 
import win32con 
 
from win32api import * 
# Try and use XP features, so we get alpha-blending etc. 
try: 
 from winxpgui import * 
except ImportError: 
 from win32gui import * 
 
 
class PyNOTIFYICONDATA: 
 _struct_format = ( 
 "I" # DWORD cbSize; structure size (in bytes)
 "I" # HWND hWnd; handle of the window that handles the message
 "I" # UINT uID; unique identifier
 "I" # UINT uFlags; 
 "I" # UINT uCallbackMessage; the message received by the window that handles the message
 "I" # HICON hIcon; Tray icon handle
 "128s" # TCHAR szTip[128]; Tip text
 "I" # DWORD dwState; tray icon state
 "I" # DWORD dwStateMask; state mask
 "256s" # TCHAR szInfo[256]; bubble tip text
 "I" # union { 
  # UINT uTimeout; time for the balloon prompt to disappear (milliseconds)
  # UINT uVersion; version (0 for V4, 3 for V5)
  # } DUMMYUNIONNAME; 
 "64s" # TCHAR szInfoTitle[64]; balloon tip title
 "I" # DWORD dwInfoFlags; balloon hint icons
 ) 
 _struct = (_struct_format) 
 
 hWnd = 0 
 uID = 0 
 uFlags = 0 
 uCallbackMessage = 0 
 hIcon = 0 
 szTip = '' 
 dwState = 0 
 dwStateMask = 0 
 szInfo = '' 
 uTimeoutOrVersion = 0 
 szInfoTitle = '' 
 dwInfoFlags = 0 
 
 def pack(self): 
 return self._struct.pack( 
  self._struct.size, 
  , 
  , 
  , 
  , 
  , 
  ("gbk"), 
  , 
  , 
  ("gbk"), 
  , 
  ("gbk"), 
  
 )
 
 def __setattr__(self, name, value): 
 # avoid wrong field names 
 if not hasattr(self, name): 
  raise (NameError, name) 
 self.__dict__[name] = value 
 
class MainWindow: 
 def __init__(self):
 # Initialize variables
  =""
  =""
 =5# 5 seconds delay
  =None
  =None
  = False
 #()
 
 
 def creWind(self):
  # Register the Window class.
 wc = WNDCLASS() 
  =  = GetModuleHandle(None) 
  = "PythonTaskbarDemo" # Strings are fine as long as they have a value, as do the following 3 places
  = { win32con.WM_DESTROY:  } # could also specify a wndproc. 
 classAtom = RegisterClass(wc)
 # Create the Window. 
 style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU 
  = CreateWindow(classAtom, "Taskbar Demo", style, 
  0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 
  0, 0, , None 
 )
 UpdateWindow()
 #
 def startBubble(self,title, msg, duration=3):
 
 if(==None):
  ()
  =title
 =msg
 =duration
 
 iconPathName = ((, ()+"\\")) 
 icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE 
 try: 
  hicon = LoadImage(, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) 
 except: 
  hicon = LoadIcon(0, win32con.IDI_APPLICATION) 
 flags = NIF_ICON | NIF_MESSAGE | NIF_TIP 
 nid = (, 0, flags, win32con.WM_USER + 20, hicon, "Balloon tooltip demo") 
 try:
  Shell_NotifyIcon(NIM_ADD, nid)
 except:
  ==None
 self.show_balloon(, )
 
 ()
 #ReleaseDC(,wc)
 #DeleteDC(wc)
 try:
  DestroyWindow()
  ==None
 except:
  return None
 
 
 def show_balloon(self, title, msg): 
 # For this message I can't use the win32gui structure because 
 # it doesn't declare the new, required fields
 
 nid = PyNOTIFYICONDATA() 
  =  
  = NIF_INFO 
 
 # type of balloon and text are random 
 # = NIIF_INFO 
  = msg[:64]
  = title[:256] 
 
 # Call the Windows function, not the wrapped one 
 from ctypes import windll 
 Shell_NotifyIcon = windll.shell32.Shell_NotifyIconA 
 Shell_NotifyIcon(NIM_MODIFY, ()) 
 
 def OnDestroy(self, hwnd, msg, wparam, lparam): 
 nid = (, 0) 
 Shell_NotifyIcon(NIM_DELETE, nid) 
 PostQuitMessage(0) # Terminate the app. 
 
if __name__=='__main__':
 msgTitle =u"You have a short message."
 msgContent =u"hello python"
 msgTitle =msgTitle
 bubble =MainWindow()
 (msgTitle,msgContent)
 (msgTitle,u"i'm a balloon")
 (msgTitle,u"how do u feel?")

This program to modify the online program for the WINDOWS platform, interested friends can also be modified to minimize the tray program.

This is the whole content of this article.