请注意:这是一个自我回答的问题,仅供参考。
使用Python如何显示通知:
a)超时并且
b)使超时明显倒计时。
答案 0 :(得分:2)
更新:发布此消息仅两周后,我将结果更新为Mint 19(Ubuntu 18.04),下面所述的计时器功能在标准通知中消失了。我只能假设转移到GTK + 3实际上隐藏了计时器。它在那里但几乎看不见。
在控制中心->弹出通知中选择通知样式Nodoka或Coco确实可以正确显示计时器。
结束更新
使用notify2
中的标准通知模块Notify
和gi.repository
,即使您无意使用它,也只需添加一个动作即可。
注意:我发现这似乎没有记录在案的原因。
除了在通知中添加了关闭按钮外,它还提供了一个基于提供的超时时间而减少的表盘。
为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
在Notify
中使用gi.repository
:
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)