Hi all,

Following is a piece of code which tries to open an image using Tkinter, the root window opens but I am not able to see the image. I don't know what is wrong with my implementation.

Could you please have a look at the code & see if I am missing something?

from tkFileDialog import *
import Image, ImageTk,Pmw, Tkinter

class Demo:
    def __init__(self, parent):
    	notebook = Pmw.NoteBook(parent)
 	notebook.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
	page_4 = notebook.add('About')
	pg4_group = Pmw.Group(page_4, tag_text = 'Picture')
        pg4_group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
        image = Image.open("pic.JPG")
        photo = ImageTk.PhotoImage(image)
        label = Tkinter.Label(pg4_group.interior(),image=photo)
        label.place(x=0,y=0,width=image.size[0],height=image.size[1])

if __name__ == '__main__':
    root = Tkinter.Tk()
    Pmw.initialise(root)
    widget = Demo(root)
    root.mainloop()

Thanks,

Your code looks somewhat roundabout. Quick sanity check, make the Label this and see if it works:

label = Label(parent,text = 'Hi world!',background = 'yellow')

If theres a highlighted piece of text ('Hi World') your label works and it's an issue with displaying the image.

Whenever I do anything, I put in lots of print statements so I know where the code is going, where it is breaking. Start simple and build up.

Hi Zac,,

Thank you for your reply. I had tried something similar & it works perfectly fine but with images it does not work.

Regards

What does this line do?

Pmw.initialise(root)

Check if you're getting an error, run it from IDLE. Even if the window remains open there could still be an error.

OK,
After this line:

label = Tkinter.Label(pg4_group.interior(), image=photo)

try adding the following:

label.photo = photo

This will create an instance of your image in the label, preventing it from being garbage collected (Which is what I suspect is going on!).


Next, after your call to label.place, add the following:

label.pack()

Your final file should look something like this:

from tkFileDialog import *
import Image, ImageTk,Pmw, Tkinter

class Demo:
    def __init__(self, parent):
        notebook = Pmw.NoteBook(parent)
        notebook.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
        page_4 = notebook.add('About')
        pg4_group = Pmw.Group(page_4, tag_text = 'Picture')
        pg4_group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
        image = Image.open("pic.JPG")
        photo = ImageTk.PhotoImage(image)
        label = Tkinter.Label(pg4_group.interior(),image=photo)
        label.photo = photo #prevent photo from being garbage collected
        label.place(x=0,y=0,width=image.size[0],height=image.size[1])
        label.pack() # display the image
        
if __name__ == '__main__':
    root = Tkinter.Tk()
    Pmw.initialise(root)
    widget = Demo(root)
    root.mainloop()

Now your image should appear and your problems should be solved!

Cheers for now,
Jas.

P.S. See the following link for more details on the Tkinter.Label widget:
http://effbot.org/tkinterbook/label.htm

commented: nice help! +7

Hey Jas,

Thanks a lot, it worked :) After adding the two lines it runs perfectly.

Zac, thanks to you too for your time & effort.

U guys rock:)

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.