代码之家  ›  专栏  ›  技术社区  ›  Rolf of Saxony

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

  •  1
  • Rolf of Saxony  · 技术社区  · 6 年前



    b)使超时明显倒计时。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Rolf of Saxony    6 年前
    在控制中心中选择通知样式nodoka或coco-->弹出通知,是否正确显示计时器。
    对于 notify2

    def mess_callback(): 定义初始化(self,parent,caption,msg,timeout=none,emergency=none): 否则:紧急程度=0 caps=notify2.get_server_caps()。 mess.set_timeout(timeout)毫秒 #如果没有下面的“添加动作”选项,则不会显示超时的倒计时。 通知(无,“错误2”,“此消息将在默认值后超时”,超时=-1) 通知(无,“注意”,“重要信息”,超时=20000,紧急=1) notify2.uninit()取消注册 gi.需要_版本(“通知”,“0.7”)。 如果紧急:通过 img='/home/rolf/myapp.png' mess=notify.notification.new(caption,msg,img)传递图像是可选的 混乱。设置紧急(紧急)0-低,1-正常,2-关键 如果超时!=0,大写为“actions”: 消息(无,“错误2”,“此消息将在默认值后超时”,timeout=-1) 消息(无,“注意”,“重要消息”,超时=20000,紧急=1)

    在标准通知中。我只能假设移动到GTK+3实际上隐藏了计时器。它在那里,但几乎看不见。
    在控制中心中选择通知样式nodoka或coco-->弹出通知,会正确显示计时器。
    结束更新

    使用标准通知模块notify2Notifygi.repository你只需要添加一个动作,即使你不打算使用它。
    注意:我发现似乎没有任何书面的原因。
    除了在通知中添加一个关闭按钮外,它还提供一个根据提供的超时时间减少的时钟面。
    对于通知2以下内容:

    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.存储库

    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