Skip to main content
 首页 » 编程设计

Python GI Notify 如何调用 Gtk.main()

2025年05月04日11mayingbao

我正在尝试创建一个 Python 通知应用程序。简而言之,这是我想做的:
1. 查看我的gmail帐号
2.显示未读邮件数量通知
3. 显示允许我打开 Chromium 的按钮(使用系统调用)

现在一切看起来都很好。检查邮件部分很简单。我序列化了我的未读邮件计数,这样通知就不会每分钟都显示一次。它仅在我有新邮件时显示。
我阻塞的地方是我不知道如何创建主 gtk 循环以便我可以处理按钮信号。

这是我的代码:

#!/usr/bin/python 
 
 
from gi.repository import Notify, Gtk, GLib 
from urllib.request import FancyURLopener 
from datetime import datetime, date, time, timedelta 
import os.path, sys, getopt 
from subprocess import call 
 
serialisedvalue=0; 
serialiseddate=0; 
 
def callback(): 
        call(["chromium", "gmail.com"]) 
 
def serialise(unread): 
        try: 
                f = open("mailcount", "w") 
                try: 
                        f.write(unread+"\n") # Write a string to a file 
                        f.write(datetime.now().strftime('%b %d %Y %I:%M%p')) 
                finally: 
                        f.close() 
        except IOError: 
                pass 
 
def deserialise(): 
        global serialisedvalue 
        global serialiseddate 
        try: 
                f = open("mailcount", "r") 
                try: 
                        serialisedvalue = f.readline().rstrip() 
                        serialiseddate = datetime.strptime(f.readline(), '%b %d %Y %I:%M%p') 
                finally: 
                        f.close() 
        except IOError: 
                pass 
 
def notif(unread): 
        Notify.init ("New Mail") 
        if unread != "1": 
                Hello=Notify.Notification.new ("New mail","You have "+unread+" unread mails","/usr/share/icons/Faenza/actions/96/mail-forward.png") 
        else : 
                Hello=Notify.Notification.new ("New mail","You have "+unread+" unread mails","/usr/share/icons/Faenza/actions/96/mail-forward.png") 
        Hello.add_action('action', 'Read', callback, None, None) 
        Hello.show () 
 
def main(argv): 
 
        notify=0 
        forced=0 
        try: 
                opts, args = getopt.getopt(argv,"nf",['notify','force-notify']) 
        except getopt.GetoptError: 
                print("unreadgmail.py [-n --notify] [-f --force-notify") 
                sys.exit(2) 
        for opt,args in opts: 
                if opt in ("-n", "--notify"): 
                        notify=1 
                elif opt in ("-f","--force-notify"): 
                        forced=1 
        url = 'https://%s:%s@mail.google.com/mail/feed/atom' % ("myaccount", "mypassword") 
        opener = FancyURLopener() 
        page = opener.open(url) 
        contents = page.read().decode('utf-8') 
        ifrom = contents.index('<fullcount>') + 11 
        ito   = contents.index('</fullcount>') 
        unread = contents[ifrom:ito] 
        print("Unread messages : "+unread) 
        if notify==1 and forced==0: 
                if os.path.exists("mailcount"): 
                        deserialise() 
                else: 
                        serialise(unread) 
                        deserialise() 
                if unread != "0": 
                        if unread != serialisedvalue: 
                                notif(unread) 
                                serialise(unread) 
                        elif ((datetime.now() - serialiseddate) > timedelta(hours=1)): 
                                notif(unread) 
        if forced==1: 
                notif(unread) 
        GLib.MainLoop().run() 
 
 
 
 
if __name__ == "__main__": 
   main(sys.argv[1:]) 

我记得我的通知曾经在 pygtk 和 pynotify 上运行良好。虽然我想更新我的代码并且因为我丢失了最后一个代码,但我对此一无所知。在我的 main 中调用 Gtk.main() 只是阻止程序直到我杀死它。

我正在使用 Gnome3.6、Archlinux 和 python3.3。
那么有谁知道如何在程序结束前“等待”信号处理程序被点击?事实上它运行良好,但脚本只是在显示通知时结束并且不等待信号。

非常感谢:)

编辑:我的问题的更多细节:
如您所见,程序已经结束并且没有等待信号。这就是我现在要解决的问题。

请您参考如下方法:

如果您没有使用 GTK+——据我所知,您没有使用——您可能可以在程序结束时调用 GLib.MainLoop().run() main 函数来保持你的程序运行。