簡體   English   中英

Python GI通知如何調用Gtk.main()?

[英]Python GI Notify How can I call the Gtk.main()?

我正在嘗試創建一個Python通知應用程序。 簡而言之,這就是我想要做的:
1.檢查我的Gmail帳戶
2.顯示帶有未讀郵件數量的通知
3.顯示一個允許我打開鉻的按鈕(使用系統調用)

現在一切看起來都很好。 支票郵件部分很容易。 我序列化了我的未讀郵件計數,以使通知不會每分鍾顯示一次。 僅在有新郵件時顯示。
我要阻塞的地方是我不知道如何創建主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函數末尾調用GLib.MainLoop().run()來保持程序運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM