winotify通知点击回调后重复弹出及隐藏命令提示符问题求助
解决winotify通知重复弹出与命令提示符显示问题
问题1:点击按钮后通知重复弹出
核心问题有两点:
- App ID不统一:代码里
Registry用的app_id="myapp"和Notification用的app_id="308046B0AF4A39CB"不一致,Winotify需要两者统一才能正确管理通知生命周期与回调逻辑。 - 回调与类绑定及启动顺序问题:未正确处理类方法的回调绑定,且
notifier.start()提前启动可能导致重复触发通知。
修复步骤:
- 将两处
app_id统一为同一个值(比如保留"myapp")。 - 把
toast_callback改为类的静态方法,避免实例绑定冲突。 - 调整启动顺序:先创建Toast实例显示通知,再启动
notifier监听回调。
问题2:点击按钮显示命令提示符
这是因为默认用python.exe运行脚本会弹出控制台窗口,解决方式二选一:
- 用
pythonw.exe启动脚本:把脚本后缀改成.pyw,或者直接执行pythonw your_script.py。 - 打包成无控制台EXE:用
pyinstaller工具,执行pyinstaller --noconsole --onefile your_script.py生成无窗口的可执行文件。
修改后的完整代码
from winotify import Notification, audio, Notifier, Registry import webbrowser import json # 统一App ID APP_ID = "myapp" registry = Registry(app_id=APP_ID, script_path=__file__) notifier = Notifier(registry) class Toast: def __init__(self): self.notify() @staticmethod @notifier.register_callback def toast_callback(): webbrowser.open('http://google.co.kr', new=2) return json.dumps({'message': 'link has been clicked'}) def notify(self): toast = Notification( app_id=APP_ID, title="Message Title", msg="Hello World", duration="short" ) toast.set_audio(audio.Reminder, loop=False) toast.add_actions( label="Button text", launch=notifier.callback_to_url(Toast.toast_callback) ) toast.show() if __name__ == '__main__': # 先显示通知,再启动回调监听 myapp = Toast() notifier.start()
内容的提问来源于stack exchange,提问作者unice
相关产品推荐
相关产品推荐

