简体   繁体   English

Python中的简单Gnome面板小程序

[英]Simple Gnome Panel Applet in Python

When I'm on the train to work I connect my netbook to my Nexus One's wifi hotspot. 当我在火车上班时,我将上网本连接到我的Nexus One的wifi热点。 As I go through a tunnel my phone obviously loses it's 3G connection and takes a while to re-establish once the train emerges. 当我通过隧道时,我的手机显然会失去它的3G连接,并且在火车出现后需要一段时间重新建立。 But the netbook wifi logo stays constant as it's still connected to the phone itself. 但上网本wifi标识保持不变,因为它仍然连接到手机本身。

I've written a little python program that attempts to ping a server and thus decides if internet is available (feel free to suggest a method of detecting internet connection that would be either quicker or use less bandwidth as I am capped per month). 我写了一个小python程序,试图ping服务器,从而决定互联网是否可用(随意建议一种检测互联网连接的方法,可以更快或使用更少的带宽,因为我每月上限)。

My question is: how can I create an applet for GNOME Panel 2.30.2 in Python, to graphically display this status, so I can decide when to continue clicking links and expecting internet to work. 我的问题是:如何在Python中为GNOME Panel 2.30.2创建一个applet,以图形方式显示这个状态,这样我就可以决定何时继续点击链接并期望互联网正常工作。

I got this example with a panel button to work but would like an icon that changes depending on the situation. 我得到了这个带有面板按钮的示例 ,但是想要一个根据情况而变化的图标。

I've used Python for a few years haven't but coded gnome before. 我已经使用Python几年了但之前没有编码gnome。 I'm using the ubuntu desktop edition as my login rather than unity, on 10.04. 我在10.04上使用ubuntu桌面版作为我的登录而不是统一。

Check out this simple applet I made . 看看我制作的这个简单的applet It has an icon that changes depending on events. 它有一个图标,根据事件而变化。 Simply replace the logic with your logic and it should do the trick. 只需用您的逻辑替换逻辑,它应该可以解决问题。 Even better, it should be compatible with all freedesktop-compatible environments. 更好的是,它应该与所有与freedesktop兼容的环境兼容。

For future reference, a really nice guide on how to build indicators for Gnome3: http://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html 有关如何为Gnome3构建指标的非常好的指南: http ://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html

Complete source code: 完整源代码:

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