📜 ⬆️ ⬇️

Gnome Applets. Introduction

An applet is a small application that is integrated into the Gnome panel and performs some step-by-step actions. In this series of posts, I want to tell you how these gnome applets are created, from simple to something more complex and interesting. We will use PyGTK to create applets, but in principle we can write applets for Gnome using the C + GTK + bundle. Usually an applet consists of two parts: - Description of the applet itself and its actions in any programming language (in our case python) - Description file of meta-information (file with the extension .server, containing meta-information of the applet) Let's proceed to the practice. Let's create for the beginning the simplest applet, which will not be of any practical use, but from the theoretical part it is important, since it gives general notions about the structure of applets. The applet will be a simple button that can be added to the panel, click on this button, in principle, all the functionality of this simple application, in the following posts we turn to more interesting, and most importantly useful things. We will create the SimpleApplet.py file in which will contain the functionality of our applet. Applet Code:
Copy Source | Copy HTML<br/> #!/usr/bin/env python <br/> <br/> import pygtk<br/> import sys<br/>pygtk.require( '2.0' )<br/> <br/> import gnomeapplet<br/> import gtk<br/> <br/> def factory (applet, iid):<br/> button = gtk.Button()<br/> button.set_relief(gtk.RELIEF_NONE)<br/> button.set_label( "Simple Button" )<br/> button.connect( "button_press_event" , showMenu , applet)<br/> applet.add(button)<br/> applet.show_all()<br/> return True<br/> <br/> def showMenu (widget, event, applet):<br/> if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3 :<br/> widget.emit_stop_by_name( "button_press_event" )<br/> create_menu (applet)<br/> <br/> def create_menu (applet):<br/> propxml= """ <br/> <popup name="button3"> <br/> <menuitem name="Item 3" verb="About" label="_About" pixtype="stock" pixname="gtk-about"/> <br/> </popup>""" <br/> verbs = [( "About" , showAboutDialog )]<br/> applet.setup_menu(propxml, verbs, None)<br/> <br/> def showAboutDialog (*arguments, **keywords):<br/> pass <br/> <br/> if len ( sys .argv) == 2 :<br/> if sys .argv[ 1 ] == "run-in-window" :<br/> mainWindow = gtk.Window(gtk.WINDOW_TOPLEVEL)<br/> mainWindow.set_title( "Ubuntu System Panel" )<br/> mainWindow.connect( "destroy" , gtk.main_quit)<br/> applet = gnomeapplet.Applet()<br/> factory (applet, None)<br/> applet.reparent(mainWindow)<br/> mainWindow.show_all()<br/> gtk.main()<br/> sys .exit()<br/> <br/> if __name__ == '__main__' :<br/> print "Starting factory" <br/> gnomeapplet.bonobo_factory( "OAFIID:Gnome_Panel_Example_Factory" , gnomeapplet.Applet.__gtype__, "Simple gnome applet example" , "1.0" , factory )<br/> <br/>
Now for the meta-information of our applet. Create a file with the .server extension and name it SimpleApplet.server. This file will contain in XML format the description of the meta-information of our applet (the displayed icon, the path to the executable .py file, etc.) For different applets, the main framework .server remains unchanged. SimpleApplet.server:
Copy Source | Copy HTML<br/> < oaf_info > <br/> < oaf_server iid ="OAFIID:Gnome_Panel_Example_Factory" type ="exe" location ="/home/SimpleApplet.py"> <br/> < oaf_attribute name ="repo_ids" type ="stringv"> <br/> < item value ="IDL:Bonobo/GenericFactory:1.0"/> <br/> < item value ="IDL:Bonobo/Unknown:1.0"/> <br/> </ oaf_attribute > <br/> < oaf_attribute name ="name" type ="string" value ="Gnome Applet Example"/> <br/> < oaf_attribute name ="description" type ="string" value ="Simple gnome applet example"/> <br/> </ oaf_server > <br/> < oaf_server iid ="OAFIID:Gnome_Panel_Example" type ="factory" location ="OAFIID:Gnome_Panel_Example_Factory"> <br/> < oaf_attribute name ="repo_ids" type ="stringv"> <br/> < item value ="IDL:GNOME/Vertigo/PanelAppletShell:1.0"/> <br/> < item value ="IDL:Bonobo/Control:1.0"/> <br/> < item value ="IDL:Bonobo/Unknown:1.0"/> <br/> </ oaf_attribute > <br/> < oaf_attribute name ="name" type ="string" value ="Example"/> <br/> < oaf_attribute name ="description" type ="string" value ="Simple gnome applet example"/> <br/> < oaf_attribute name ="panel:category" type ="string" value ="Utility"/> <br/> < oaf_attribute name ="panel:icon" type ="string" value ="computer.png"/> <br/> </ oaf_server > <br/> </ oaf_info > <br/>
Here in princep and all. 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 context menu of the panel. The result should be an applet in the Gnome panel that looks like this: image

')

Source: https://habr.com/ru/post/73973/


All Articles