9

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.

1
  • I am asking it to set the program icon of the current window using "self" Commented Dec 11, 2010 at 11:06

3 Answers 3

9

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()
Sign up to request clarification or add additional context in comments.

6 Comments

Nope it doesn't work. The thing is when I try the status icon image, I just give the filepath as the image name and it works perfectly. It is just that with the program icon it doesn't. I tried giving the entire path name right from the root. Still cant get it to work.
I think I have some sample code at work, so I'll whittle that down and post it tomorrow.
I executed your code, however at first it could not find the icon.png file. So I gave it the explicit path "\home\username\images\icon.png", then the program ran but still did not show the icon at the top left corner...
@Nik (1) Those should be forward slashes "/" or the path should be constructed from os.path.join (2) what version of GTK and PyGTK are you using?
You could, instead of using this whole own function also use os.path.abspath()
|
2

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)

2 Comments

Why are you talking about system tray icons?
Because you didn't specify what icon you had in mind and I wanted to help you.My example is an icon for system task bar. Hope it helped.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.