Python实现Windows上气泡提醒效果的方法

作者:xm1331305 时间:2021-08-13 07:58:46 

本文实例讲述了Python实现Windows上气泡提醒效果的方法。分享给大家供大家参考。具体实现方法如下:


# -*- encoding: gbk -*-
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; 结构大小(字节)
 "I" # HWND hWnd; 处理消息的窗口的句柄
 "I" # UINT uID; 唯一的标识符
 "I" # UINT uFlags;
 "I" # UINT uCallbackMessage; 处理消息的窗口接收的消息
 "I" # HICON hIcon; 托盘图标句柄
 "128s" # TCHAR szTip[128]; 提示文本
 "I" # DWORD dwState; 托盘图标状态
 "I" # DWORD dwStateMask; 状态掩码
 "256s" # TCHAR szInfo[256]; 气泡提示文本
 "I" # union {
   #  UINT uTimeout; 气球提示消失时间(毫秒)
   #  UINT uVersion; 版本(0 for V4, 3 for V5)
   # } DUMMYUNIONNAME;
 "64s" #  TCHAR szInfoTitle[64]; 气球提示标题
 "I" # DWORD dwInfoFlags; 气球提示图标
)
_struct = struct.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,
  self.hWnd,
  self.uID,
  self.uFlags,
  self.uCallbackMessage,
  self.hIcon,
  self.szTip,
  self.dwState,
  self.dwStateMask,
  self.szInfo,
  self.uTimeoutOrVersion,
  self.szInfoTitle,
  self.dwInfoFlags
 )
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, title, msg, duration=3):
 # Register the Window class.
 wc = WNDCLASS()
 hinst = wc.hInstance = GetModuleHandle(None)
 wc.lpszClassName = "PythonTaskbarDemo"
 # 字符串只要有值即可,下面3处也一样
 wc.lpfnWndProc = { win32con.WM_DESTROY: self.OnDestroy }
 # could also specify a wndproc.
 classAtom = RegisterClass(wc)
 # Create the Window.
 style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
 self.hwnd = CreateWindow(classAtom, "Taskbar Demo", style,
  0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
  0, 0, hinst, None
 )
 UpdateWindow(self.hwnd)
 iconPathName = os.path.abspath(os.path.join(sys.prefix, "pyc.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, "Balloon tooltip demo")
 Shell_NotifyIcon(NIM_ADD, nid)
 self.show_balloon(title, msg)
 time.sleep(duration)
 DestroyWindow(self.hwnd)
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()
 nid.hWnd = self.hwnd
 nid.uFlags = NIF_INFO
 # type of balloon and text are random
 nid.dwInfoFlags = NIIF_INFO
 nid.szInfo = msg[:64]
 nid.szInfoTitle = title[:256]
 # Call the Windows function, not the wrapped one
 from ctypes import windll
 Shell_NotifyIcon = windll.shell32.Shell_NotifyIconA
 Shell_NotifyIcon(NIM_MODIFY, nid.pack())
def OnDestroy(self, hwnd, msg, wparam, lparam):
 nid = (self.hwnd, 0)
 Shell_NotifyIcon(NIM_DELETE, nid)
 PostQuitMessage(0) # Terminate the app.
if __name__=='__main__':
MainWindow("您有一条短消息", "您该睡觉了")

希望本文所述对大家的Python程序设计有所帮助。

标签:Python,Windows
0
投稿

猜你喜欢

  • Python统计时间内的并发数代码实例

    2022-02-17 18:24:16
  • 找到个很好的例子导出excel的

    2008-09-28 13:12:00
  • Django之模板层的实现代码

    2022-11-10 11:40:54
  • python函数装饰器构造和参数传递

    2023-05-24 16:49:17
  • 利用python汇总统计多张Excel

    2023-12-31 14:22:11
  • 使用 Python 在京东上抢口罩的思路详解

    2023-06-01 01:10:30
  • 从p开始,循序渐进学习WEB标准

    2008-03-08 18:53:00
  • Python中BeautifuSoup库的用法使用详解

    2023-11-19 04:52:48
  • selenium python浏览器多窗口处理代码示例

    2023-11-20 07:09:29
  • 利用phpExcel实现Excel数据的导入导出(全步骤详细解析)

    2023-06-22 02:56:42
  • Python torch.fft.rfft()函数用法示例代码

    2022-02-15 02:03:36
  • SQL Server 作业的备份(备份作业非备份数据库)

    2012-07-11 15:58:49
  • python快速建立超简单的web服务器的实现方法

    2021-03-14 23:25:14
  • VS2008 和.NET 3.5 Beta2常见问题的解决方案

    2007-09-23 12:33:00
  • WEB标准与XHTML 1.0 Transitional等文档类型介绍

    2007-10-20 21:18:00
  • 任意定制文本对齐方式:CSS Text Wrapper

    2008-02-03 11:11:00
  • 如何设置PyCharm中的Python代码模版(推荐)

    2022-12-14 03:56:29
  • [译]如何设计网页小广告(banner)

    2009-10-16 20:40:00
  • 价值3亿美元的按钮[译]

    2009-03-18 19:39:00
  • Python连接Hadoop数据中遇到的各种坑(汇总)

    2023-09-13 20:16:34
  • asp之家 网络编程 m.aspxhome.com