Strange problem with Tkinter... photos don't show on first iteration.

A

aldonnelley

Hi all.

Just having a weird problem with tkinter. I'm trying to make a gui that
shows results from an image search, with a "forward" and "back" button
so the user can compare results from different pages. All that's
working fine...
The problem I'm having is that the images don't show onscreen the first
time the "first page" of results shows up. When I click the "search
again" button, and have more than the original results page to toggle
between, the images __do__ show up on the "first page" of results. (and
on the second, etc, etc.)
Which is to say, there's no error messages, and on the first "page",
the first time it's shown, the Toplevel formats correctly, and there
are spaces where the images should be that are the correct size,
just... no images.
It's baffling me. Perhaps someone can help.

Cheers, Al.

Script attached (apologies for it not being as neat as it probably
should be... :)
################################
from Tkinter import *
import sys
import cPickle

#opens gui and displays returned image thumbnails
class Im_Res1:
def __init__(self, master, lof, pageno):
self.master = root
root.withdraw()
nav_child(master, lof, pageno)
self.master.mainloop()

# the child window class
class nav_child:
def __init__(self, master, lof, pageno):
self.slave = Toplevel(master)
self.slave.grid()
self.slave.title("Image Results")
self.picts = []
lif = lof[pageno]
lenlif = len(lif)
for r in range(lenlif):
j = lif[r]
f = j[2]
self.img = PhotoImage(file = f)
self.picts.append(self.img)
print "self.picts = ", self.picts
for g in range(lenlif):
text = (lif[g])[1]# label text
def callback(text=text):

x=win32com.client.Dispatch('InternetExplorer.Application.1')
x.Visible=1
x.Navigate(text)
p = open("VerRes.txt", 'w')
p.write(text)
p.close()
root.destroy()
if 0<= g < 6:
buttonx = Button(self.slave, text=(text), height=1,
width=23, command=callback)
buttonx.grid(row=3, column=g)
Label(self.slave, text=((lif[g])[0])).grid(row=2,
column=g)
self.Im = self.picts[g]
Label(self.slave, image=self.Im).grid(row=1, column=g)
elif 6<= g < 12:
buttonx = Button(self.slave, text=(text), height=1,
width=23, command=callback)
buttonx.grid(row=6, column=(g-6))
Label(self.slave, text=((lif[g])[0])).grid(row=5,
column=(g-6))
self.Im = self.picts[g]
Label(self.slave, image=self.Im).grid(row=4,
column=(g-6))
elif 12<= g < 18:
buttonx = Button(self.slave, text=(text), height=1,
width=23, command=callback)
buttonx.grid(row=9, column=(g-12))
Label(self.slave, text=((lif[g])[0])).grid(row=8,
column=(g-12))
self.Im = self.picts[g]
Label(self.slave, image=self.Im).grid(row=7,
column=(g-12))
elif 18<= g < 24:
buttonx = Button(self.slave, text=(text), height=1,
width=23, command=callback)
buttonx.grid(row=12, column=(g-18))
Label(self.slave, text=((lif[g])[0])).grid(row=11,
column=(g-18))
self.Im = self.picts[g]
Label(self.slave, image=self.Im).grid(row=10,
column=(g-18))
def search_again(lof=lof, pageno=pageno):
p = open("VerRes.txt", 'w')
pageno = pageno + 1
reslist = [pageno, 1, lof]
cPickle.dump(reslist, p)
p.close()
root.destroy()
SearchAgain = Button(self.slave, text='Search Again.',
fg="blue", command=search_again)
SearchAgain.grid(row=13, column=2, columnspan=2)
if pageno > 0:
def Back_Cmd(lof=lof, pageno=pageno):
pageno = pageno - 1
nav_child(master, lof, pageno)
self.slave.destroy()
backbutton = Button(self.slave, text="Previous Page",
height=1, width=23, command=Back_Cmd)
backbutton.grid(row=13, column=0)
if pageno < (len(lof) - 1):
def Fwd_Cmd(lof=lof, pageno=pageno):
pageno = pageno + 1
nav_child(master, lof, pageno)
self.slave.destroy()
fwdbutton = Button(self.slave, text="Next Page", height=1,
width=23, command=Fwd_Cmd)
fwdbutton.grid(row=13, column=5)
def Quit_Search():
root.destroy()
sys.exit(0)
quitbutton = Button(self.slave, text='QUIT', fg="red",
command=Quit_Search)
quitbutton.grid(row=14, column=2, columnspan=2)

#############################################
"lof" is nested lists 'cos there's more than one page of more than one
image-
Images don't show on the first results page... *sigh*
Obviously, after the second iteration of the search, "lof" would look
more like:
[[["Some_Name", "http://some-url.net",
"C:\Image_File.GIF"]][["Other_Name", "http://Other-url.net",
"C:\Other_Image_File.GIF"]]]
##############################################
# run mainloop.

lof = [[["Some_Name", "http://some-url.net", "C:\Image_File.GIF"]]]

root = Tk()
app = Im_Res1(root, lof, 0)
root.mainloop()
########################################################
 
F

Fredrik Lundh

Just having a weird problem with tkinter. I'm trying to make a gui that
shows results from an image search, with a "forward" and "back" button
so the user can compare results from different pages. All that's
working fine...
The problem I'm having is that the images don't show onscreen the first
time the "first page" of results shows up. When I click the "search
again" button, and have more than the original results page to toggle
between, the images __do__ show up on the "first page" of results. (and
on the second, etc, etc.)
Which is to say, there's no error messages, and on the first "page",
the first time it's shown, the Toplevel formats correctly, and there
are spaces where the images should be that are the correct size,
just... no images.
It's baffling me. Perhaps someone can help.

this is explained in the Python FAQ, and also in the note at the bottom
of this page:

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

</F>
 
A

aldonnelley

Yup,
That's the problem. Can't thank you enough.
I'd read about Tkinter "garbage collection", but, like car crashes and
lung cancer, you never think it's going to happen to you...

thanks once again.
Cheers, Al.
 
A

aldonnelley

And I say once again: I can't thank you enough.

YOU ROCK!

cheers, al.

(Just changed the code in my main program, and it WORKS! The previous
thankyou was only a preliminary.)
ps I really like python, too. :)
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top