国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現Windows上氣泡提醒效果的方法

2020-01-04 18:07:28
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Python實現Windows上氣泡提醒效果的方法,涉及Python針對windows窗口操作的相關技巧,需要的朋友可以參考下

本文實例講述了Python實現Windows上氣泡提醒效果的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. # -*- encoding: gbk -*-  
  2. import sys  
  3. import os  
  4. import struct  
  5. import time  
  6. import win32con  
  7. from win32api import * 
  8. # Try and use XP features, so we get alpha-blending etc.  
  9. try:  
  10. from winxpgui import * 
  11. except ImportError:  
  12. from win32gui import * 
  13. class PyNOTIFYICONDATA:  
  14. _struct_format = (  
  15. "I" # DWORD cbSize; 結構大小(字節)  
  16. "I" # HWND hWnd; 處理消息的窗口的句柄  
  17. "I" # UINT uID; 唯一的標識符  
  18. "I" # UINT uFlags;  
  19. "I" # UINT uCallbackMessage; 處理消息的窗口接收的消息  
  20. "I" # HICON hIcon; 托盤圖標句柄  
  21. "128s" # TCHAR szTip[128]; 提示文本  
  22. "I" # DWORD dwState; 托盤圖標狀態  
  23. "I" # DWORD dwStateMask; 狀態掩碼  
  24. "256s" # TCHAR szInfo[256]; 氣泡提示文本  
  25. "I" # union {  
  26. # UINT uTimeout; 氣球提示消失時間(毫秒)  
  27. # UINT uVersion; 版本(0 for V4, 3 for V5)  
  28. # } DUMMYUNIONNAME;  
  29. "64s" # TCHAR szInfoTitle[64]; 氣球提示標題  
  30. "I" # DWORD dwInfoFlags; 氣球提示圖標  
  31. )  
  32. _struct = struct.Struct(_struct_format)  
  33. hWnd = 0 
  34. uID = 0 
  35. uFlags = 0 
  36. uCallbackMessage = 0 
  37. hIcon = 0 
  38. szTip = ''  
  39. dwState = 0 
  40. dwStateMask = 0 
  41. szInfo = ''  
  42. uTimeoutOrVersion = 0 
  43. szInfoTitle = ''  
  44. dwInfoFlags = 0 
  45. def pack(self):  
  46. return self._struct.pack(  
  47. self._struct.size,  
  48. self.hWnd,  
  49. self.uID,  
  50. self.uFlags,  
  51. self.uCallbackMessage,  
  52. self.hIcon,  
  53. self.szTip,  
  54. self.dwState,  
  55. self.dwStateMask,  
  56. self.szInfo,  
  57. self.uTimeoutOrVersion,  
  58. self.szInfoTitle,  
  59. self.dwInfoFlags  
  60. )  
  61. def __setattr__(self, name, value):  
  62. # avoid wrong field names  
  63. if not hasattr(self, name):  
  64. raise NameError, name  
  65. self.__dict__[name] = value  
  66. class MainWindow:  
  67. def __init__(self, title, msg, duration=3):  
  68. # Register the Window class.  
  69. wc = WNDCLASS()  
  70. hinst = wc.hInstance = GetModuleHandle(None)  
  71. wc.lpszClassName = "PythonTaskbarDemo" 
  72. # 字符串只要有值即可,下面3處也一樣  
  73. wc.lpfnWndProc = { win32con.WM_DESTROY: self.OnDestroy } 
  74. # could also specify a wndproc.  
  75. classAtom = RegisterClass(wc)  
  76. # Create the Window.  
  77. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU  
  78. self.hwnd = CreateWindow(classAtom, "Taskbar Demo", style,  
  79. 00, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,  
  80. 00, hinst, None 
  81. )  
  82. UpdateWindow(self.hwnd)  
  83. iconPathName = os.path.abspath(os.path.join(sys.prefix, "pyc.ico")) 
  84. icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE  
  85. try:  
  86. hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 00, icon_flags)  
  87. except:  
  88. hicon = LoadIcon(0, win32con.IDI_APPLICATION)  
  89. flags = NIF_ICON | NIF_MESSAGE | NIF_TIP  
  90. nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "Balloon tooltip demo")  
  91. Shell_NotifyIcon(NIM_ADD, nid)  
  92. self.show_balloon(title, msg)  
  93. time.sleep(duration)  
  94. DestroyWindow(self.hwnd)  
  95. def show_balloon(self, title, msg):  
  96. # For this message I can't use the win32gui structure because  
  97. # it doesn't declare the new, required fields  
  98. nid = PyNOTIFYICONDATA()  
  99. nid.hWnd = self.hwnd  
  100. nid.uFlags = NIF_INFO  
  101. # type of balloon and text are random  
  102. nid.dwInfoFlags = NIIF_INFO  
  103. nid.szInfo = msg[:64]  
  104. nid.szInfoTitle = title[:256]  
  105. # Call the Windows function, not the wrapped one  
  106. from ctypes import windll  
  107. Shell_NotifyIcon = windll.shell32.Shell_NotifyIconA  
  108. Shell_NotifyIcon(NIM_MODIFY, nid.pack())  
  109. def OnDestroy(self, hwnd, msg, wparam, lparam):  
  110. nid = (self.hwnd, 0)  
  111. Shell_NotifyIcon(NIM_DELETE, nid)  
  112. PostQuitMessage(0# Terminate the app.  
  113. if __name__=='__main__':  
  114. MainWindow("您有一條短消息""您該睡覺了"

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平塘县| 鹤峰县| 玛纳斯县| 来宾市| 宁晋县| 通州市| 偃师市| 鸡西市| 辽宁省| 鸡泽县| 上栗县| 新蔡县| 大兴区| 汪清县| 凌源市| 北京市| 长白| 双牌县| 利津县| 安福县| 新民市| 西峡县| 汾西县| 南雄市| 红原县| 柏乡县| 大兴区| 前郭尔| 佛教| 库伦旗| 福鼎市| 新疆| 永寿县| 娱乐| 津市市| 怀安县| 盐边县| 定结县| 遂宁市| 玉门市| 西吉县|