Gnome Applets. Introduction

    An applet is a small application that is integrated into the Gnome panel and performs any phased actions. In this series of posts, I want to tell how these very gnome applets are created, from simple to something to more complex and interesting. We will use PyGTK to create applets, but in principle, you can write applets for Gnome using the C + GTK + bundle. Typically, an applet consists of two parts: - A description of the applet itself and its actions in some programming language (python in our case) - A meta-information description file (a file with the .server extension containing the meta-information of the applet) Let's move on to practice. To begin with, we will create the simplest applet, which will not be of any practical use, but from the theoretical part it is important, since it gives general concepts about the structure of applets. The applet will be a simple button, which can be added to the panel, click on this button, and in principle all the functionality of this simple application, in the next posts we will move on to more interesting, and most importantly useful things. Let's create a file SimpleApplet.py which will contain the functionality of our applet. Applet code:
    Copy Source | Copy HTML
    #!/usr/bin/env python
     
    import pygtk
    import sys
    pygtk.require('2.0')
     
    import gnomeapplet
    import gtk
     
    def factory(applet, iid):
        button = gtk.Button()
        button.set_relief(gtk.RELIEF_NONE)
        button.set_label("Simple Button")
        button.connect("button_press_event", showMenu, applet)
        applet.add(button)
        applet.show_all()
        return True
     
    def showMenu(widget, event, applet):
        if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3:
            widget.emit_stop_by_name("button_press_event")
            create_menu(applet)
     
    def create_menu(applet):
        propxml="""
                
                
                """
        verbs = [("About", showAboutDialog)]
        applet.setup_menu(propxml, verbs, None)
     
    def showAboutDialog(*arguments, **keywords):
        pass
     
    if len(sys.argv) == 2:
        if sys.argv[1] == "run-in-window":
            mainWindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
            mainWindow.set_title("Ubuntu System Panel")
            mainWindow.connect("destroy", gtk.main_quit)
            applet = gnomeapplet.Applet()
            factory(applet, None)
            applet.reparent(mainWindow)
            mainWindow.show_all()
            gtk.main()
            sys.exit()
     
    if __name__ == '__main__':
        print "Starting factory"
        gnomeapplet.bonobo_factory("OAFIID:Gnome_Panel_Example_Factory", gnomeapplet.Applet.__gtype__, "Simple gnome applet example", "1.0", factory)
     
    Now for the meta information for our applet. Create a file with the extension .server and name it SimpleApplet.server. This file will contain in XML format a description of the meta-information of our applet (displayed icon, path to the executable .py file, etc.) For different applets, the main .server framework remains unchanged. SimpleApplet.server:
    Copy Source | Copy HTML





















    That's all in the prince. Our applet is ready, it does not know how to do anything, but its development gives a general idea of ​​applets. To add our applet to the panel, do the following: - Move the SimpleApplet.server file to the / usr / lib / bonobo / servers / directory - Make the SimpleApplet.py file executable: chmod + x SimpleApplet.py - Add the applet to the panel using The standard panel context menu As a result, you should get an applet in the Gnome panel similar to this one:image

    Also popular now: