Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a program icon in Python GTK

Tags:

python

ubuntu

I know this is very simple, just use the command self.set_icon_from_file("icon.png"), however my program still does not display the icon. I made sure the icon.png is in the same working directory as the Python file. I also tried giving the complete file path, but still it does not display the icon.

I am using Ubuntu 10.10 if that helps and using Python V2.6. I use Glade Interface Designer to design the GUI. However, I tried setting the icon both using Glade and using the command above.

I hope I have provided sufficient information.

EDIT: I got the status icon to work in my program.. However in the question I meant the program icon displayed in the task bar and also on the left side of the application bar.

like image 627
Nik Avatar asked Dec 11 '10 10:12

Nik


2 Answers

I am not sure what icon you are creating, but try this smallest PyGTK icon-showing example of taskbar icon I have thought of:

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk

# create icon object
statusIcon = gtk.StatusIcon()

# load it
statusIcon.set_from_file("icon.ico")

# show it
statusIcon.set_visible(True)

# and run main gtk loop
gtk.main()

Maybe you just missed the command statusIcon.set_visible(True)

like image 43
Martin Kosek Avatar answered Nov 01 '22 15:11

Martin Kosek


I made sure the icon.png is in the same working directory of the python file.

This may be your problem — paths are looked up relative to the working directory of the Python interpreter, not the file containing the code. I often find myself defining a function like:

def get_resource_path(rel_path):
    dir_of_py_file = os.path.dirname(__file__)
    rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
    abs_path_to_resource = os.path.abspath(rel_path_to_resource)
    return abs_path_to_resource

Mine isn't actually quite that verbose, but hopefully the variable names make it clear what's going on. Also, getting the absolute path isn't strictly necessary, but might help if you need to debug.

Then you can just do:

self.set_icon_from_file(get_resource_path("icon.png"))

Update: Here is a demo program. "icon.png" is in the same directory as this script, and I run it using ./gtktest.py. I see the icon in the top left corner (standard place for my theme). icon.png is just a shape drawn in Inkscape and exported as a bitmap (it works with the original SVG too, anyway).

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:

    def delete_event(self, widget, event, data=None):
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        # create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.set_icon_from_file('icon.png')

        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        # Creates a new button with the label "Hello World".
        self.button = gtk.Button("Hello World")

        self.window.add(self.button)
        self.button.show()
        self.window.show()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()
like image 97
detly Avatar answered Nov 01 '22 13:11

detly