Tkinter PhotoImage won't show ;-(

V

vm

please help!

I can't find anything wrong (except the result ofc ;-)

This:



picfile = 'logo.gif'

pic = tk.PhotoImage(file=picfile, master=root)

canpic = cv.create_image(320, 240, image=pic) #cv is is a Tkinter
Canvas ofc



....works just fine, but when I put it in the event binding function, it
doesn't work,

it doesn't report any errors too, I'm sure the function went all the way
through

because I print the test-text at the end of the function... But it doesn't
show picture!



here's the entire program I wrote to test this out:

===================================================

import Tkinter as tk, os



root = tk.Tk()

root.title = 'Picviewer'



###setting Listbox, Canvas

lb = tk.Listbox(root,width =20, height=22)

lb.grid(column=0, row=0, columnspan = 2)



cv = tk.Canvas(root, width=640, height=480, bg='white')

cv.grid(column=2,row=0, rowspan=2)



###listing all .gifs in current working directory into the Listbox lb

cwd = os.getcwd()

filelista = os.listdir(cwd)

filelista.sort()

for element in filelista:

if os.path.isfile(element) and '.gif' in element.lower(): lb.insert(
lb.size() ,element)



###Load-button bind function (showing picture) - the problem seems to be in
here somewhere I think

def putpic(event):

picfile = lb.selection_get()

pic = tk.PhotoImage(file=picfile, master=root)

canpic = cv.create_image(320, 240, image=pic)

###just to see what's going on

print str(picfile), "\n", str(pic), '\n', str(canpic)



###setting Load-button

load = tk.Button(root, text='Load', width=16)

load.bind('<Button-1>', putpic)

load.grid(column=0, row=2, columnspan=2)



###Putting the logo image at start (more like test image at start - and it
works)

pic = tk.PhotoImage(file='logo.gif', master=root)

canpic = cv.create_image(320, 240, image=pic)



root.mainloop()

================================================

Maybe I just need a fresh pair of eyes, SOMEONE!
 
F

Fredrik Lundh

vm said:
I can't find anything wrong (except the result ofc ;-)

This:

picfile = 'logo.gif'

pic = tk.PhotoImage(file=picfile, master=root)

canpic = cv.create_image(320, 240, image=pic) #cv is is a Tkinter
Canvas ofc

...works just fine, but when I put it in the event binding function, it
doesn't work,

this is explained in the python faq, and towards the end of this
page:

http://effbot.org/tkinterbook/photoimage.htm

</F>
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top