在Windows 11中生成多个通知,而无需等待每个通知以继续运行主要代码
我在Windows 11上产生了基本通知:
from win32api import *
from win32gui import *
import win32con
import sys, os
import struct
import time
class WindowsBalloonTip:
def __init__(self, title, msg, timeout):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
}
wc = WNDCLASS()
hinst = wc.hInstance = GetModuleHandle(None)
wc.lpszClassName = "PythonTaskbar"
wc.lpfnWndProc = message_map
classAtom = RegisterClass(wc)
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",title,200,msg))
time.sleep(timeout)
DestroyWindow(self.hwnd)
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
Shell_NotifyIcon(NIM_DELETE, nid)
PostQuitMessage(0)
def balloon_tip(title, msg, timeout):
w=WindowsBalloonTip(msg, title, timeout)
if __name__ == '__main__':
balloon_tip(sys.argv[1], sys.argv[2], float(sys.argv[3]))
我每30秒每30秒同时保持两个单独的代码,并且全天,如果它们都同时生成通知(要缩短问题中的代码,我没有放置循环部分,只是子过程调用):
code_1.py
:
import subprocess
import sys
subprocess.Popen([sys.executable, "balloontip.py", 'alala', 'code 1', '1'])
code_2.py
:
import subprocess
import sys
subprocess.Popen([sys.executable, "balloontip.py", 'alala', 'code 2', '1'])
遇到此错误:
Python WNDPROC handler failed
Traceback (most recent call last):
File "C:\Users\Computador\Desktop\Tests\balloontip.py", line 42, in OnDestroy
Shell_NotifyIcon(NIM_DELETE, nid)
pywintypes.error: (-2147467259, 'Shell_NotifyIcon', 'Unspecified error')
并在某些地方说单个代码Point试图为通知创建多个呼叫:
等待每个通知完成执行时,找不到错误:
import subprocess
import sys
one = '1'
two = '2'
three = '3'
if one == '1':
p1 = subprocess.Popen([sys.executable, "balloontip.py", 'alala', one, '1'])
p1.wait()
if two == '2':
p2 = subprocess.Popen([sys.executable, "balloontip.py", 'alala', two, '1'])
p2.wait()
if three == '3':
p3 = subprocess.Popen([sys.executable, "balloontip.py", 'alala', three, '1'])
p3.wait()
# rest of code...
但是我迫不及待地等待通知执行完成代码,因此我需要删除等待
:
import subprocess
import sys
one = '1'
two = '2'
three = '3'
if one == '1':
p1 = subprocess.Popen([sys.executable, "balloontip.py", 'alala', one, '1'])
if two == '2':
p2 = subprocess.Popen([sys.executable, "balloontip.py", 'alala', two, '1'])
if three == '3':
p3 = subprocess.Popen([sys.executable, "balloontip.py", 'alala', three, '1'])
# rest of code...
遇到此错误:
Python WNDPROC handler failed
Traceback (most recent call last):
File "C:\Users\Computador\Desktop\Tests\balloontip.py", line 42, in OnDestroy
Shell_NotifyIcon(NIM_DELETE, nid)
pywintypes.error: (-2147467259, 'Shell_NotifyIcon', 'Unspecified error')
有没有办法接收这些通知而不必等待每个通知才能完成运行?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论