Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have created the default ubuntu-application template using quickly and I'm trying to work out how to change the displayed image through code.

The default image shows an ubuntu logo with the path ".../media/background.png" within glade.

If I open the projects window python file (testWindow.py as the projects name is test) and add the following lines at the base of the finish_initializing function (changing nothing else within the default files created),

self.PNG = self.builder.get_object("image1")
self.PNG.set_from_file("../media/background.png")

The program runs but instead of the ubuntu logo, the small missing file icon displays. However no error message displays in the terminal and changing the png file to another png, or moving it to the same directory as the python file (changing the second lines file to "background.png") gives the same issue. I can use the clear() function to remove the image successfully but I'm clearly doing something wrong and I can't seem to make any headway into the documentation for the image widget.

In case it helps to know what I'm aiming to do, I'm trying to create a simple flashcard program, displaying a Japanese character and requiring the user to input the correct pronunciation and displaying another image of a new character if the user is successful.

Thank you for any help you can give!

share|improve this question
1  
It's probably a current path issue (Glade file vs Python script). Add some debug prints in your code, like print os.path.exists("../media/background.png") and print os.getcwd(). –  Timo Aug 5 '13 at 19:41

1 Answer 1

up vote 0 down vote accepted

The path you're giving set_from_file is relative to the glade file but not the python file. It's better to use full paths anyways as using relative paths often ends up having issues when you run the installed application. Quickly provides a nice helper function to give you the full path for a file found in the data/ folder. Try:

from test_lib.testconfig import get_data_file

[....]   

    self.image = self.builder.get_object("image1")
    self.background = get_data_file("media/background.png")
    self.image.set_from_file(self.background)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.