有没有办法禁用 win10toast python 库上的通知声音?

Bra*_*ley 3 python audio notifications toast windows-10

我正在使用 win10toast 为 Windows 制作通知弹出窗口。有什么办法可以让通知静音吗?换句话说,我可以禁用我正在创建的通知的声音吗?我可以更改声音吗?

编辑:添加示例代码

我的示例代码:

from win10toast import ToastNotifier


toaster = ToastNotifier()
for i in range(0,70000000):
    pass
toaster.show_toast("Hey User",
                   "The program is running pretty well. You should try to disable audio on me next though!",
                   icon_path=None,
                   duration=5)
Run Code Online (Sandbox Code Playgroud)

rob*_*991 5

您必须修改库的源代码才能执行此操作。转到安装库的文件夹并打开“__init__.py”文件。在顶部,放置所有“win32gui”导入后,写入from win32gui import NIIF_NOSOUND.

之后,转到第 107 行,您应该看到这段代码:

Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO,
                                  WM_USER + 20,
                                  hicon, "Balloon Tooltip", msg, 200,
                                  title))
Run Code Online (Sandbox Code Playgroud)

在“title”参数之后,放置“NIIF_NOSOUND”,它应该如下所示:

Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO,
                                  WM_USER + 20,
                                  hicon, "Balloon Tooltip", msg, 200,
                                  title, NIIF_NOSOUND))
Run Code Online (Sandbox Code Playgroud)

如果您想执行此操作,则必须进一步修改源代码,可以向show_toast方法添加新参数。像这样的东西:

# line 121
def show_toast(self, title="Notification", msg="Here comes the message",
                icon_path=None, duration=5, threaded=False, sound=False):
Run Code Online (Sandbox Code Playgroud)

并进一步发送“声音”参数:

 # line 130
 if not threaded:
     self._show_toast(title, msg, icon_path, duration, sound)
 else:
     if self.notification_active():
         # We have an active notification, let is finish so we don't spam them
         return False

     self._thread = threading.Thread(target=self._show_toast, args=(title, msg, icon_path, duration, sound))
     self._thread.start()
 return True
Run Code Online (Sandbox Code Playgroud)

然后还将参数添加到“隐藏”_show_toast方法中:

# line 63
def _show_toast(self, title, msg,
                icon_path, duration, sound):
Run Code Online (Sandbox Code Playgroud)

并创建一个 if else 语句来检查是否应该添加“NIIF_NOSOUND”标志:

# line 107
Shell_NotifyIcon(NIM_ADD, nid)
data = (self.hwnd, 0, NIF_INFO,
            WM_USER + 20,
            hicon, "Balloon Tooltip", msg, 200,
            title)
if not sound:
    data = data + (NIIF_NOSOUND,)
Shell_NotifyIcon(NIM_MODIFY, data)
Run Code Online (Sandbox Code Playgroud)

此参数需要 InfoFlags 的组合,用于修改通知的行为和外观。阅读有关NIIF_NOSOUND标志和其他标志的更多信息。在这里您可以看到“pywin32” pywin32 文档中提供了哪些“NIIF”标志。

Shell_NotifyIcon您可以在此处查看有关函数参数的更多信息pywin32 Shell_NotifyIcon

函数的第二个参数Shell_NotifyIcon是一个元组,表示一个“PyNOTIFYICONDATA”对象,它采用不同的参数,您可以在此处查看有关该对象的更多信息pywin32 PyNOTIFYICONDATA

注意:这对我在 Windows 10 上有效。