Writing an Applet for GNOME in Python
I publish this post at the request of the respected VladX , who had a temporary misunderstanding with karma.
An applet is a small application that can be embedded directly into the GNOME panel. Typically, an applet performs some specific narrow functionality (changing the volume, mounting devices), which compares it favorably with a bulky window application. In fact, it is not difficult for a knowledgeable Python coder to write their own applet, but there is a solution for those who don’t know: a large number of useful (and not so) applets can be found on the gnomefiles.org website , in addition, there are examples of writing applets in C. on the official website .
So we are writing an applet. An applet must do something. Our applet, for example, will show habrakarma and habrasil. To get all this information, we will use the Habr API .
This is how our applet begins. Everything is clear here - we import the necessary libraries, we enter the necessary parameters into the variables. To avoid any problems with Russian characters, we will use the UTF-8 encoding (and by the way, I advise you to always do the same).
This, in fact, is the most interesting part of the applet. The constructor method __init__ () initializes the object, the __init_popupmenu () method adds the Open and About commands to the context menu and sets the corresponding callback method. The get_info () method receives a response from the server and processes it for the cherished numbers of karma and strength. It would be foolish to use a special xml parser in such a trivial task, so we’ll do it in a simple quick-coder way.
These functions are the same in most Python applets, you only need to change the arguments passed to the bonobo_factory method to your own.
There is also one useful point: when passing the --run-in-window parameter to the script, the applet runs in a separate window, which helps a lot when debugging.
Well, that would seem to be all. But no - the applet is written, but it is not in the list of gnome applets. To fix this, you need to put the file (in this case, you can use the name gnomeMyAppletFactory.server ) in the directory / usr / lib64 / bonobo / servers with something like this:
Do not forget to change the path to the script to your own and allow its execution using chmod + x .
Good coding,% username%!
ps In preparing the material, the highlight.hohli.com resource was used .
An applet is a small application that can be embedded directly into the GNOME panel. Typically, an applet performs some specific narrow functionality (changing the volume, mounting devices), which compares it favorably with a bulky window application. In fact, it is not difficult for a knowledgeable Python coder to write their own applet, but there is a solution for those who don’t know: a large number of useful (and not so) applets can be found on the gnomefiles.org website , in addition, there are examples of writing applets in C. on the official website .
Go!
So we are writing an applet. An applet must do something. Our applet, for example, will show habrakarma and habrasil. To get all this information, we will use the Habr API .
#! / usr / bin / env python
# coding = utf-8
import sys, os, gtk, gtk.gdk, pygtk, gnomeapplet, gnome
pygtk.require ('2.0')
__USER__ = 'VladX'
__URL__ = 'http: / /habrahabr.ru/api/profile/ '+ __USER__
This is how our applet begins. Everything is clear here - we import the necessary libraries, we enter the necessary parameters into the variables. To avoid any problems with Russian characters, we will use the UTF-8 encoding (and by the way, I advise you to always do the same).
class MyApplet (gnomeapplet.Applet):
def __init__ (self, applet, iid):
self.applet = applet
self.applet.set_name ('MyApplet')
self.hbox = gtk.HBox ()
self.applet.add (self. hbox)
self.event = gtk.EventBox ()
self.hbox.add (self.event)
self.info = gtk.Label ()
self.event.add (self.info) # So that the object can respond to various events, its need to be placed in the Event Box
self.event.set_tooltip_text ('Habraiser' + __USER__)
self.event.connect ('button-press-event', self.callback_button)
self .__ init_popupmenu ()
self.applet.connect ('destroy', self.callback_destroy)
self.applet.show_all () # Show all this in the panel
self.info.set_text (self.get_info ())
def __init_popupmenu (self):
self.applet.setup_menu ('' '
This, in fact, is the most interesting part of the applet. The constructor method __init__ () initializes the object, the __init_popupmenu () method adds the Open and About commands to the context menu and sets the corresponding callback method. The get_info () method receives a response from the server and processes it for the cherished numbers of karma and strength. It would be foolish to use a special xml parser in such a trivial task, so we’ll do it in a simple quick-coder way.
def applet_factory (applet, iid):
MyApplet (applet, iid)
return True
def main (args):
if len (sys.argv) == 2 and sys.argv [1] == 'run-in-window':
window = gtk.Window (gtk.WINDOW_TOPLEVEL)
window.set_title ('It works!')
window.connect ('destroy', gtk.main_quit)
applet = gnomeapplet.Applet ()
applet_factory (applet, None)
applet.reparent (window)
window.show_all ()
gtk.main ()
sys.exit ()
elif len (sys.argv) == 2 and sys.argv [1] == 'help':
'' 'Print a help by parameters'' '
print' ''
--run-in-window - run applet independent of gnome-panel
--help - show this message '' '
else:
gnomeapplet.bonobo_factory (' OAFIID: GNOME_MyApplet_Factory ',
MyApplet .__ gtype__,
' My Applet ',
' 1.0 ',
applet_factory)
if __name__ == '__main__':
main (sys.argv)
These functions are the same in most Python applets, you only need to change the arguments passed to the bonobo_factory method to your own.
There is also one useful point: when passing the --run-in-window parameter to the script, the applet runs in a separate window, which helps a lot when debugging.
Well, that would seem to be all. But no - the applet is written, but it is not in the list of gnome applets. To fix this, you need to put the file (in this case, you can use the name gnomeMyAppletFactory.server ) in the directory / usr / lib64 / bonobo / servers with something like this:
Do not forget to change the path to the script to your own and allow its execution using chmod + x .
Good coding,% username%!
ps In preparing the material, the highlight.hohli.com resource was used .