簡體   English   中英

Python中的簡單Gnome面板小程序

[英]Simple Gnome Panel Applet in Python

當我在火車上班時,我將上網本連接到我的Nexus One的wifi熱點。 當我通過隧道時,我的手機顯然會失去它的3G連接,並且在火車出現后需要一段時間重新建立。 但上網本wifi標識保持不變,因為它仍然連接到手機本身。

我寫了一個小python程序,試圖ping服務器,從而決定互聯網是否可用(隨意建議一種檢測互聯網連接的方法,可以更快或使用更少的帶寬,因為我每月上限)。

我的問題是:如何在Python中為GNOME Panel 2.30.2創建一個applet,以圖形方式顯示這個狀態,這樣我就可以決定何時繼續點擊鏈接並期望互聯網正常工作。

我得到了這個帶有面板按鈕的示例 ,但是想要一個根據情況而變化的圖標。

我已經使用Python幾年了但之前沒有編碼gnome。 我在10.04上使用ubuntu桌面版作為我的登錄而不是統一。

看看我制作的這個簡單的applet 它有一個圖標,根據事件而變化。 只需用您的邏輯替換邏輯,它應該可以解決問題。 更好的是,它應該與所有與freedesktop兼容的環境兼容。

有關如何為Gnome3構建指標的非常好的指南: http ://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html

完整源代碼:

import signal
import json

from urllib2 import Request, urlopen, URLError

from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Notify as notify


APPINDICATOR_ID = 'myappindicator'

def main():
    indicator = appindicator.Indicator.new(APPINDICATOR_ID, 'sample_icon.svg', appindicator.IndicatorCategory.SYSTEM_SERVICES)
    indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
    indicator.set_menu(build_menu())
    notify.init(APPINDICATOR_ID)
    gtk.main()

def build_menu():
    menu = gtk.Menu()
    item_joke = gtk.MenuItem('Joke')
    item_joke.connect('activate', joke)
    menu.append(item_joke)
    item_quit = gtk.MenuItem('Quit')
    item_quit.connect('activate', quit)
    menu.append(item_quit)
    menu.show_all()
    return menu

def fetch_joke():
    request = Request('http://api.icndb.com/jokes/random?limitTo=[nerdy]')
    response = urlopen(request)
    joke = json.loads(response.read())['value']['joke']
    return joke

def joke(_):
    notify.Notification.new("<b>Joke</b>", fetch_joke(), None).show()

def quit(_):
    notify.uninit()
    gtk.main_quit()

if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    main()

暫無
暫無

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

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