在Windows 11中生成多个通知,而无需等待每个通知以继续运行主要代码

发布于 2025-02-07 22:12:39 字数 3855 浏览 0 评论 0 原文

我在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')

有没有办法接收这些通知而不必等待每个通知才能完成运行?

I generate a basic notifications on my Windows 11 like this:

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

I keep two separate codes running simultaneously in a loop every 30 seconds and throughout the day, in case they both generate a notification at the same time (to shorten the codes in the question, I didn't put the looping part, just the subprocess call):

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'])

This error is encountered:

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

And let's say a single code at some point tries to create multiple calls to the notifications:

When waiting for each of the notifications to finish executing, no error is found:

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...

But I can't wait for the notifications executions finish to continue the code, so I need to remove the wait:

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...

This error is encountered:

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

Is there a way to receive these notifications without having to wait for each one to finish running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文