gpt4 book ai didi

Python 显示带有可见超时的通知

转载 作者:行者123 更新时间:2023-12-01 01:48:56 25 4
gpt4 key购买 nike

请注意:这是一个 self 回答的问题,仅供引用。

使用 Python 如何显示通知:
a) 超时并且
b) 有明显的超时倒计时。

最佳答案

更新:发布此文后仅两周,我更新到 Mint 19 (Ubuntu 18.04),结果,下面描述的计时器功能在标准通知中消失了。我只能假设转向 GTK+3 实际上隐藏了计时器。它就在那里,但几乎看不见。
在控制中心 --> 弹出通知中选择通知样式 Nodoka 或 Coco,确实可以正确显示计时器。
结束更新

使用gi.repository中的标准通知模块notify2Notify,只需添加一个操作,即使您有无意使用它。
注意:我发现似乎没有记录原因。 除了向通知添加关闭按钮之外,它还提供了一个根据提供的超时而减少的表盘。
对于notify2:

import notify2

class Notify():
def mess_callback():
pass

def __init__(self,parent,caption,msg,timeout=None,urgency=None):
if timeout != None: pass
else: timeout = 0 # message should not timeout

if urgency: pass
else: urgency = 0

img = '/home/rolf/MyApp.png'
caps = notify2.get_server_caps()
mess = notify2.Notification(caption,msg,img) # passing an image is optional
mess.set_timeout(timeout) #milliseconds
mess.set_urgency(urgency) #0-Low, 1-Normal, 2-Critical
# Without the following `add_action` option, No countdown to the time out is shown
if timeout != 0 and 'actions' in caps:
mess.add_action("close","Close",self.mess_callback,None) #Show the countdown to close
mess.show()

if __name__ == "__main__":
notify2.init("MyApp") #Register MyApp

Notify(None,"Error","This message is not timed and has to be manually cancelled")
Notify(None,"Error 2","This message will timeout after the default value",timeout=-1)
Notify(None,"Information","An Unimportant message",timeout=20000,urgency=0)
Notify(None,"Attention","An Important message",timeout=20000,urgency=1)
Notify(None,"Emergency","A Critical message",timeout=20000,urgency=2)

notify2.uninit() #Un-register

gi.repository中使用Notify:

import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

class Message():
def mess_callback():
pass

def __init__(self,parent,caption,msg,timeout=None,urgency=None):
if timeout != None: pass
else: timeout = 0 # message should not timeout

if urgency: pass
else: urgency = 0

img = '/home/rolf/MyApp.png'
caps = Notify.get_server_caps()
mess = Notify.Notification.new(caption, msg, img) # passing an image is optional
mess.set_timeout(timeout) #milliseconds
mess.set_urgency(urgency) #0-Low, 1-Normal, 2-Critical
# Without the following `add_action` option, No countdown to the time out is shown
if timeout != 0 and 'actions' in caps:
mess.add_action("close","Close",self.mess_callback,None) #Show the countdown to close
mess.show()

if __name__ == "__main__":
Notify.init("MyApp") #Register MyApp

Message(None,"Error","This message is not timed and has to be manually cancelled")
Message(None,"Error 2","This message will timeout after the default value",timeout=-1)
Message(None,"Information","An Unimportant message",timeout=20000,urgency=0)
Message(None,"Attention","An Important message",timeout=20000,urgency=1)
Message(None,"Emergency","A Critical message",timeout=20000,urgency=2)

enter image description here

关于Python 显示带有可见超时的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50933000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com