Developing a panel applet

i am developing a panel applet in python . The applet shows on the list of adding applets to panel window but when I add it it shows only a white dot . The script runs just fine . When launching it does not give any error . Can somebody help me ? Here is the code :

#!/usr/bin/env python

import gi

gi.require_version(“Gtk”, “2.0”)

from gi.repository import Gtk
from gi.repository import MatePanelApplet

# create the applet

def applet_fill(applet):

Button = Gtk.ToggleButton("Button on panel")
applet.add(Button)
applet.show_all()

# this is called by mate-panel on applet creation

def applet_factory(applet, iid, data):
if iid != “MateUserMenuApplet”:
return False
applet_fill(applet)
return True

MatePanelApplet.Applet.factory_main(“MateUserMenuAppletFactory”, True,
MatePanelApplet.Applet.gtype,
applet_factory, None)

Do you have the command linked to the pixmap icon?. :smiley:

I did not understand the question .

You said it is only showing a white dot, do you have an icon associated with the command. :smiley:

It is not a command it is applet for the panel that I am making . But for some reason it shows only white dot not the Gtk.ToggleButton .

That is what I meant, does your command link to that button?. :smiley:

I have nothing to link becouse I am creating a new Gtk.ToggleButton as it is in the script .

I’ve recently been developing a panel applet in python and have got used to see the white dot. As you might have guessed it means that something has gone wrong somewhere and the applet hasn’t initialised properly. After looking at your code I have one suggestion - after

gi.require_version(“Gtk”, “2.0”)

add:

gi.require_version(“MatePanelApplet”, “4.0”)

This is something I had to add to my applet after a recent update to Mate.

To help with debugging in future, what you can do is start your applet from the command line in a terminal window (e.g. python applet.py) and then add your applet to the panel as normal. Any output from the applet - error messages or print statements - will then appear in the terminal.

By the way, the code of my applet is available at github and you may find it helpful. I don’t claim to be an expert at programming in python or gtk, - I’m definitely not - but at least my code is up to date and works on recent versions of Mate, which is sadly not the case for most of the applet examples and tutorials you’ll find on the internet.

Hope this is of some use to you…

1 Like

You are the maker of mate-dock applet cool . I am fan of your work . The thing is that when i run it from terminal it does not hang the terminal . Thanks for your response I will try it when I have acces to my laptop .

1 Like

@IvCHo,

@robint99 said it soooooo much better than I have done, I did not realise you hadn’t finished the applet button!. :smiley:

@robint99 I have added the version requirment to the program but it still did not work . But in unknown reason the script gives me this error

That message you’re seeing is perfectly normal and you’ll see it every time you run your app from the command line. Its just a warning that some of the Gtk2 functionality is not available to python. Don’t let this worry you - most things work absolutely fine and the only major functionality I’ve found missing is drag and drop,

You say that your applet still isn’t working, despite the code running and I can think of a couple of reasons why this might be. One thing I forgot to mention in my other message is that if you get the white dot on the panel or your applet doesn’t seem to appear, you need to make sure that your code isn’t still running before you test a new version. There are a couple of easy ways to do this - killall UserMenu.py will do it for you (don’t reload the applet if Mate asks if you want to), or you can create a new panel just for your applet and then delete the panel whenever you need to stop your non-working running version. One other thing, don’t forget that after running your code in a terminal window you need to then add your applet to a panel to actually see it.

If none of this works for you then post your code here and I’ll take a look at it…

Don’t let these initial difficulties frustrate you too much - once you’ve actually got something to appear on the panel it starts getting a lot easier :smiley:

I am woried about "need to free the control here " warning .
It appeared after I started the code and add it to the panel .
Actually I have only 3 files . The factory file , the service file and UserMenu.py
I wanted first to see if it will run on simple basis as I am beginner programmer . Once I made a label work on mate 1.8 . But now i dunno wath is wrong .
Here is the code :

#!/usr/bin/env python3

import gi

gi.require_version("Gtk", "2.0")
gi.require_version("MatePanelApplet","4.0")

from gi.repository import Gtk
from gi.repository import MatePanelApplet

# create the applet

def applet_fill(applet):

Button = Gtk.ToggleButton("Button on panel")
applet.add(Button)
applet.show_all()

# this is called by mate-panel on applet creation
def applet_factory(applet, iid, data):
if iid != "MateUserMenuApplet":
return False
applet_fill(applet)
return True

MatePanelApplet.Applet.factory_main("MateUserMenuAppletFactory", True,
MatePanelApplet.Applet.gtype,
applet_factory, None)

#Here is the factory file -- org.mate.panel.MateUserMenuApplet.mate-panel-applet :

[Applet Factory]
Id=MateUserMenuAppletFactory
InProcess=false
Location=/usr/bin/env python3 /home/ivalin/mate-user-menu-applet/UserMenu.py
Name=MATE User Menu Applet Factory
Description=Applet to show a user menu

[MateUserMenuApplet]
Name=MATE User Menu Applet
Description=Applet to show a user menu
Icon=mate
MateComponentId=OAFIID:MATE_MateUserMenuApplet;

#Here is the service file -- org.mate.panel.applet.MateUserMenuAppletFactory.service :

[D-BUS Service]
Name=org.mate.panel.applet.MateUserMenuAppletFactory
Exec=/usr/bin/env python3 /home/ivalin/mate-user-menu-applet/UserMenu.py

END

I have a MainWindow.py that I will connect to the button but I think it is not nessecary since I did not include the module in UserMenu.py

I have it working, and the only problem was in UserMenu.py at this line:

MatePanelApplet.Applet.factory_main("MateUserMenuAppletFactory", True,
MatePanelApplet.Applet.gtype,
applet_factory, None)

which should be:

MatePanelApplet.Applet.factory_main("MateUserMenuAppletFactory", True,
MatePanelApplet.Applet.__gtype__,
applet_factory, None)

And here is the proof that it works:

I'm a bit puzzled because when I first ran your applet from the command line I got an error message about .gtype not being recognised, but you didn't. For some strange reason I also didn't get the message about the control needing to be freed...

Well actualy it was _gtype_ when i copy-pasted it it was gtype becouse of the underscores .
I finally got it to show : Here is wath i did
commented out the if and left only applet_fill(applet)
def applet_factory(applet, iid, data):
# if iid != “MateUserMenuApplet”:
# return False
applet_fill(applet)
# return True

I do not understand wath this lines do . Can you explain ,
Thank you @robint99 for the help
And to @wolfman for taking interest

1 Like

Everything is clear suddenly. The applet_factory function is not indented, and indentation is very important in Python. You must use it. [This page] (http://www.diveintopython.net/getting_to_know_python/indenting_code.html) explains better than I can how indentation works in Python...

When I copy/pasted your code I noticed the lack of indentation and just assumed that it had happened when you uploaded the code. Anyway, the applet_factory function needs to change so that it looks like this:

def applet_factory(applet, iid, data):
    if iid != "MateUserMenuApplet":
        return False

    applet_fill(applet)
    return True

This is what my UserMenu.py looks like:

To answer your question about what this code does, it's called whenever a new applet is added to the panel. If the id of the applet being created doesn't match the id you setup for your applet in the factory file, then it exits and returns False to Mate to let it know that it didn't do anything. If the id matches, then the applet_fill function is called and True is returned to Mate.

Hope this all helps :smiley:

1 Like