微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Gnome Shell中使用通知的Python程序不起作用

我正在写一个Python程序,它从网页获取信息,并在Gnome Shell的Notification中显示。 我正在使用Arch,所以我想在启动时启动这个程序,如果网页上有任何改变,它会通知我。 这是我的代码

import time import webbrowser import requests from bs4 import BeautifulSoup from gi.repository import Notify,GLib IPS = {'Mobifone': True,'Viettel': False,'Vinaphone': False} LINK = "https://id.vtc.vn/tin-tuc/chuyen-muc-49/tin-khuyen-mai.html" def set_ips_state(ips_name,state): global IPS for key in IPS.iterkeys(): if key == ips_name: IPS[key] = state def call_webbrowser(notification,action_name,link): webbrowser.get('firefox').open_new_tab(link) def create_notify(summary,body,link): Notify.init("Offer") noti = Notify.Notification.new(summary,'dialog-@R_701_4045@ion') noti.add_action('action_click','Read more...',call_webbrowser,link) noti.show() # GLib.MainLoop().run() def save_to_file(path_to_file,string): file = open(path_to_file,'w') file.write(string) file.close() def main(): global IPS global LINK result = [] offer_news = open('offer_news.txt') tag_in_file = BeautifulSoup(offer_news.readline(),'html.parser') tag = tag_in_file.a offer_news.close() page = requests.get(LINK) soup = BeautifulSoup(page.text,'html.parser') for div in soup.find_all('div','tt_dong1'): # first_a = div.a # main_content = first_a.find_next_subling('a') main_content = div.find_all('a')[1] for k,v in IPS.iteritems(): if v: if main_content.text.find(k) != -1: result.append(main_content) print result[1].encode('utf-8') if tag_in_file == '': pass else: try: old_news_index = result.index(tag) print old_news_index for idx in range(old_news_index): create_notify('Offer News',result[idx].text.encode('utf-8'),result[idx].get('href')) print "I'm here" except ValueError: pass offer_news = open('offer_news.txt','w') offer_news.write(result[0].__str__()) offer_news.close() if __name__ == '__main__': while 1: main() time.sleep(10)

问题是,当我点击通知中的“Read more …”button时,它不会打开Firefox,除非我在create_notify函数中取消注释GLib.MainLoop().run() ,但这会使程序冻结。 有人可以帮忙吗?

GUI应用程序通常使用三个主要组件:小部件,事件循环和回调。 当您启动该应用程序时,您将创建小部件,注册回调并启动事件循环。 事件循环是无限循环,从小部件(如“点击按钮”)中查找事件,并触发相应的回调。

现在,在你的应用程序中你有另一个无限循环,所以这两个不会一起玩。 相反,你应该使用GLib.MainLoop().run()来触发事件。 您可以使用GLib.timeout_add_seconds来触发周期性事件,例如每10秒钟一次。

第二个问题是你需要参照一个应该调用回调函数通知。 在noti.show() GLib.MainLoop().run()之后添加GLib.MainLoop().run() noti.show()是,对noti.show()引用仍然存在,但是如果您按照之前的建议进行更改,它将不起作用。 如果您确定始终只有一个通知处于活动状态,则可以保留对上次通知的引用。 否则,你需要一个列表,并定期清除它或沿线的东西。

下面的例子应该让你在正确的方向:

from gi.repository import GLib,Notify class App(): def __init__(self): self.last_notification = None Notify.init('Test') self.check() def check(self): self.last_notification = Notify.Notification.new('Test') self.last_notification.add_action('clicked','Action',self.notification_callback,None) self.last_notification.show() GLib.timeout_add_seconds(10,self.check) def notification_callback(self,notification,data): print(action_name) app = App() GLib.MainLoop().run()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐