简体   繁体   English

python中的通知而不停止脚本

[英]Notification in python without stopping the script

I was writing code using python and opencv.我正在使用 python 和 opencv 编写代码。 If no face is detected it should notify the windows10 user.如果没有检测到人脸,它应该通知 windows10 用户。 I used win10toast我用的是win10toast

import time
from win10toast import ToastNotifier

notif = ToastNotifier()

notif.show_toast(title= "Nusrat", msg= "One baby is missing")
time.sleep(10)

but it stops the code when notification is being shown.但在显示通知时它会停止代码。 Is there any way I can show notification using gui or anything but that will not stop the code?有什么方法可以使用 gui 或任何东西显示通知,但不会停止代码?

The library's Github repo shows how to avoid blocking in the landing page example :库的 Github 存储库展示了如何避免登录页面示例中的阻塞:

from win10toast import ToastNotifier
import time

toaster = ToastNotifier()

toaster.show_toast("Example two",
               "This notification is in it's own thread!",
               icon_path=None,
               duration=5,
               threaded=True)
# Wait for threaded notification to finish
while toaster.notification_active(): time.sleep(0.1)

You need to add the threaded=True parameter.您需要添加threaded=True参数。 Sleeping is only needed if you want to check whether the notification is still active.仅当您想检查通知是否仍处于活动状态时才需要睡眠。

Displaying notification is not a blocking operation.显示通知不是阻塞操作。 The library's code forces a sleep equal to duration before closing the window :库的代码在关闭窗口之前强制睡眠等于duration

# take a rest then destroy
sleep(duration)
DestroyWindow(self.hwnd)
UnregisterClass(self.wc.lpszClassName, None)

threaded=True will execute the show/sleep/destroy flow in a separate thread. threaded=True将在单独的线程中执行show/sleep/destroy流程。 Frankly, there are cleaner ways to do that, eg using a one-off timer.坦率地说,有更简洁的方法可以做到这一点,例如使用一次性计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM