Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Gnome Panel Applets in Python

  1. #1
    Join Date
    Sep 2006
    Beans
    17

    Gnome Panel Applets in Python

    Hi, I'd like to get started in writing my own applet for the gnome-panel in python, but I really have no idea where to begin (or to finish, for that matter). I've searched for tutorials and references, but they seem obfuscated, deprecated, or unfinished. I believe the most recent I could find was from 2004, and its code is outdated (uses gnome.applet instead of gnomeapplet for instance).

    I am completely new to coding for gnome, so I don't have a solid idea of what to do, basically I'm starting from square one. So, my question is if anyone would be willing to help me start (first by writing a basic applet, maybe a hello world) and/or help me with the api and/or point me to a tutorial or how-to that is up-to-date that I can use for this purpose.

    For me, at this point, I think simplicity is paramount!

  2. #2
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: Gnome Panel Applets in Python

    I will try to help when I get back from work.

    You could look into a working program like "ubuntu system panel" or "gimmie" how they add themselves to the panel.

  3. #3
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: Gnome Panel Applets in Python

    OK.. I have looked into how this works and this example came out:

    gnomeAppletExample.py
    Code:
    #!/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("ExampleButton")
    	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="""
    			<popup name="button3">
    			<menuitem name="Item 3" verb="About" label="_About" pixtype="stock" pixname="gtk-about"/>
    			</popup>"""
    	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)
    You also have to define a .server file which you have to put into "/usr/lib/bonobo/servers".
    gnomeAppletExample.server
    Code:
    <oaf_info>
    
    	<oaf_server iid="OAFIID:Gnome_Panel_Example_Factory" type="exe" location="/home/quikee/projects/examples/python/various/gnomeAppletExample.py">
    		<oaf_attribute name="repo_ids" type="stringv">
    			<item value="IDL:Bonobo/GenericFactory:1.0"/>
    			<item value="IDL:Bonobo/Unknown:1.0"/>
    		</oaf_attribute>
    		<oaf_attribute name="name" type="string" value="Gnome Applet Example"/>
    		<oaf_attribute name="description" type="string" value="Simple gnome applet example"/>
    	</oaf_server>
    
    	<oaf_server iid="OAFIID:Gnome_Panel_Example" type="factory" location="OAFIID:Gnome_Panel_Example_Factory">
    		<oaf_attribute name="repo_ids" type="stringv">
    			<item value="IDL:GNOME/Vertigo/PanelAppletShell:1.0"/>
    			<item value="IDL:Bonobo/Control:1.0"/>
    			<item value="IDL:Bonobo/Unknown:1.0"/>
    		</oaf_attribute>
    		<oaf_attribute name="name" type="string" value="Example"/>
    		<oaf_attribute name="description" type="string" value="Simple gnome applet example"/>
    		<oaf_attribute name="panel:category" type="string" value="Utility"/>
    		<oaf_attribute name="panel:icon" type="string" value="computer.png"/>
    	</oaf_server>
    
    </oaf_info>
    You have to define the exact path to the executable. Maybe it also works if you only define the executable name and the executable is in path but I haven't checked this. The executable must have +x rights.

    In this file you also define how to represent the program in "Add to panel" dialog (you define name, description, icon).

    I hope it helps...

  4. #4
    Join Date
    Sep 2006
    Beans
    17

    Re: Gnome Panel Applets in Python

    Quikee, thanks for the fast and helpful response, I've got my applet going now...I should be able to take it from here since it seems that applets are only slightly different from regular gnome apps, and there are tons of good pygtk references out there.

    My question now is, is it:
    a) possible to create a gradient in gtk instead of resorting to create gradient images
    b) possible to set this gradient (or an image/pixmap) as the background to an element (I want to have a gradient behind a Label, so I would likely have to attach this to an EventBox or something similar)

  5. #5
    Join Date
    Sep 2006
    Beans
    17

    Re: Gnome Panel Applets in Python

    Those previous questions are still unanswered for me, and I have another one. I am now using threading to update information from the internet so I can maintain a responsive GUI while the applet is searching. When I run the applet in a window from the command line, everything works perfectly. When I add the applet to the panel for the first time, it works perfectly also, but when I remove it and try to add it again, it stalls and throws an error, unable to attach to the panel. I then have to reboot, and it's able to be added one time, etc.

    My hunch was that this had to do with the thread never being killed and thus interfering with a new instance of the applet, so I modified the updater to run once and kill the thread. Sure enough, I was then able to remove it and add it again, with it completely functional. So, my question is how I would resolve this issue; is there a function that gets called automatically on removal of the applet that I could use to kill the update thread?
    Last edited by zanelightning; July 18th, 2007 at 02:33 AM.

  6. #6
    Join Date
    Jan 2008
    Beans
    8

    Question Re: Gnome Panel Applets in Python

    That code was really helpful... Thanks a lot! But now I need to ask this... There's anyway for me to use a .directory file on that xml? I'm trying to create an apple that have a popup menu like the applications menu.

  7. #7
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: Gnome Panel Applets in Python

    As far as I know you can't use ".directory" files in this way. You have to create the pop-up menu yourself and use something like "python gmenu" or python-xdg (both work but python-xdg is more DE neutral) to parse *.desktop, *.directory and *.menu information.



    About left button click, the following should work fine:

    PHP Code:
    def showMenu(widgeteventapplet):
        if 
    event.button == 1:
            print 
    "do something on button 1"
            
    widget.emit_stop_by_name("button_press_event"
    To help you out a little.. with the code below you can create a menu on the fly:

    PHP Code:
    def createAndShowMenu(eventmenuItems):
        
    menu gtk.Menu()
        for 
    menuItem in menuItems:
            
    item gtk.MenuItem(menuItem[0], True)
            
    item.show()
            
    item.connect"activate", *menuItem[1:])
            
    menu.add(item)
        
    menu.popupNoneNoneNoneevent.buttonevent.time 
    And use it like this to create a menu with elements: "first", "second", "third" in the "showMenu" method:

    PHP Code:

    def doFirst
    (widgetadditionalParameter):
        print 
    "do first"
    def doSecond(widget):
        print 
    "do second"
    def doThird(widget):
        print 
    "do third"

    def showMenu(widgeteventapplet):
        if 
    event.button == 1:
            
    menu = [
                [
    "first"doFirst"AdditionalParameterIfYouNeedIt"],
                [
    "second"doSecond],
                [
    "third"doThird]]
            
    createAndShowMenu(eventmenu)
            
    widget.emit_stop_by_name("button_press_event"
    NOTE! I have not checked any of this.. errors in the code are possible.

  8. #8
    Join Date
    Jan 2008
    Beans
    8

    Re: Gnome Panel Applets in Python

    Thanks a lot, but I'v tried "if event.button == 1:" it simple wont work!
    iv tried
    Code:
    def showMenu(widget, event, applet):
            global num
            widget.emit_stop_by_name("button_press_event")
            propxml="""
                            <popup name="button3">
                            <menuitem name="Item 3" verb="About" label="_About" pixtype="stock" pixname="gtk-about"/>
                            <menuitem name="file-unlock"    verb="file-unlock"      label="Unlock File"             pixtype="stock" pixname="revelation-unlock" />
                            <menuitem name="file-lock"      verb="file-lock"        label="Lock File"               pixtype="stock" pixname="revelation-lock" />
                            <menuitem name="file-reload"    verb="file-reload"      label="Reload File"             pixtype="stock" pixname="revelation-reload" />
                            <separator />
                            <menuitem name="revelation"     verb="revelation"       label="Start Revelation"        pixtype="stock" pixname="revelation-revelation" />
                            <menuitem name="prefs"          verb="prefs"            label="Preferences"             pixtype="stock" pixname="gtk-properties" />
                            <menuitem name="about"          verb="about"            label="About"                   pixtype="stock" pixname="gnome-stock-about" />
                            </popup>"""
    
            verbs = [("About", showAboutDialog)]
            applet.setup_menu(propxml, verbs, None)
    and

    Code:
    def showMenu(widget, event, applet):
            global num
            if event.button == 1:
                    propxml="""
                                    <popup name="button3">
                                    <menuitem name="Item 3" verb="About" label="_About" pixtype="stock" pixname="gtk-about"/>
                                    <menuitem name="file-unlock"    verb="file-unlock"      label="Unlock File"             pixtype="stock" pixname="revelation-unlock" />
                                    <menuitem name="file-lock"      verb="file-lock"        label="Lock File"               pixtype="stock" pixname="revelation-lock" />
                                    <menuitem name="file-reload"    verb="file-reload"      label="Reload File"             pixtype="stock" pixname="revelation-reload" />
                                    <separator />
                                    <menuitem name="revelation"     verb="revelation"       label="Start Revelation"        pixtype="stock" pixname="revelation-revelation" />
                                    <menuitem name="prefs"          verb="prefs"            label="Preferences"             pixtype="stock" pixname="gtk-properties" />
                                    <menuitem name="about"          verb="about"            label="About"                   pixtype="stock" pixname="gnome-stock-about" />
                                    </popup>"""
    
                    verbs = [("About", showAboutDialog)]
                    applet.setup_menu(propxml, verbs, None)
                    (... other part of the code... )
            
                    widget.emit_stop_by_name("button_press_event")
    It simple wont pop with the left mouse button!
    Any idea where im wrong?

  9. #9
    Join Date
    Jan 2008
    Beans
    8

    Smile Re: Gnome Panel Applets in Python

    Oh, and as you can notice I'm really trying this based on your code
    thanks

  10. #10
    Join Date
    Jan 2008
    Beans
    8

    Re: Gnome Panel Applets in Python

    Doing the menu on the fly works ( not really the way I want, but pops when I click with the left button ), but when I try applet.setup_menu(..) it wont work with the left mouse button...

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •