Nik
Nik

Reputation: 759

Adding a program icon in Python GTK

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.

Upvotes: 9

Views: 13563

Answers (3)

yPhil
yPhil

Reputation: 8357

For standard icons, use stock items, and find icons that suits your needs. that way

  • You don't have to pack icons whith your program
  • The icons change according to the user's theme and will blend nicely in her environment.

for pyGTK :

gtk.icon_theme_get_default().load_icon("folder-open", 128, 0)

Upvotes: 0

detly
detly

Reputation: 30332

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()

Upvotes: 9

Martin Kosek
Martin Kosek

Reputation: 398

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)

Upvotes: 2

Related Questions