[Tutor] New image with Tkinter?

Michael Lange klappnase at freenet.de
Mon Oct 10 22:27:47 CEST 2005


On Mon, 10 Oct 2005 11:36:22 -0600
Joseph Quigley <cpu.crazy at gmail.com> wrote:

> Hi,
> I've written an image reader that uses the PIL module.
> I have a variable that uses os.listdir('mydir') to make a list of all the
> pictures in the folder... here's what I'm talking about:
(...)
> The image won't refresh when I click on 'Next'! Any help would be
> appreciated.
> Joe
> 

Hi Joe,

the image won't refresh until you explicitely tell Tk to do so, so you would have to do some more
in newPic(); btw, it looks overly complicated to me to use a "Data" class where a simple variable
will do the trick, so I would suggest to change the code like this:

pics = os.listdir(imgDir)
pics.remove('license.txt')
pics.remove('gacor.py')
print "There are %s saved images in the image folder." % len(pics)
pic = 0

root = Tk()
root.title("GaCoR Image Browser")
app = Frame(root)
app.grid()

imgPrep = ImageTk.PhotoImage(file=os.path.join(imgDir, pics[pic]))
imgShow = Label(app, image=imgPrep).grid()
info = Label(app, text="Displaying %s" % pics[Data.pic])
info.grid()

def newPic():
    global pic
    pic = pic + 1
    imgPrep.configure(file=os.path.join(imgDir, pics[pic]))
    
Button(app, text="Next Image", command=newPic).grid()
Button(app, text="Close", command=quitProg).grid()
app.mainloop()

I hope this helps

Michael



More information about the Tutor mailing list