Tkinter, toplevel and images

P

Pedro

Hi

I'm trying to build a small application that can display some images
in a toplevel window. I have this code:

def Results(master):
from Tkinter import Toplevel, Button, Label
from PIL import ImageTk

figures = ['first.png', 'second.png']

ResultsWindow = Toplevel(master)
ResultsWindow.title('Results')

picts = [ImageTk.PhotoImage(file = x) for x in figures]

butRW = Button(ResultsWindow, text = 'CLOSE', command =
ResultsWindow.destroy)
butRW.pack()

height = sum([x.height() for x in picts])
width = picts[0].width()

y = 0
for pict in picts:
label = Label(ResultsWindow,image=pict).pack()
y += pict.height()

from Tkinter import Tk
root = Tk()
Results(root)
root.mainloop()

and I just can see a grey window with a "close" button... However,
when I try the same code with the root I can see both images... Can
anyone give me a tip?

Thanks! :)
 
A

Adam E

Hi

I'm trying to build a small application that can display some images
in a toplevel window. I have this code:

def Results(master):
from Tkinter import Toplevel, Button, Label
from PIL import ImageTk

figures = ['first.png', 'second.png']

ResultsWindow = Toplevel(master)
ResultsWindow.title('Results')

picts = [ImageTk.PhotoImage(file = x) for x in figures]

butRW = Button(ResultsWindow, text = 'CLOSE', command =
ResultsWindow.destroy)
butRW.pack()

height = sum([x.height() for x in picts])
width = picts[0].width()

y = 0
for pict in picts:
label = Label(ResultsWindow,image=pict).pack()
y += pict.height()

from Tkinter import Tk
root = Tk()
Results(root)
root.mainloop()

and I just can see a grey window with a "close" button... However,
when I try the same code with the root I can see both images... Can
anyone give me a tip?

Thanks! :)

You can try making a class for the Tkinter.Toplevel window. I don't
have PIL, but I know if you query for a widget's value when the window
is not yet displayed, it will give a nearly 0-by-0 size result.

You can also try to set a size for the Toplevel window and see if the
images display.

I have not encountered this before, but I only know what I have read
from books (like Programming Python by O'Reilly) and have assumed.

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top