[Tutor] Tkinter problem (updating labels)

Philippe Smets zilp@village.uunet.be
Fri, 29 Jun 2001 14:12:32 +0200


I'm trying to make my first program in Python. I'm also using Tkinter.
It's a little game for my 8 year old daugther.
In that game I need to place images on labels. I wrote the following code
for that:

from Tkinter import*
import Image, ImageTk, os, random

class Bord:
    def __init__(self,master):
        frame=Frame(master)
        frame.grid()
        tellerrow = 0
        tellercol = 0
        colorlist =
['red','green','yellow','blue','orange','pink','brown','gold', 'brown']
        self.labelist = {}
        self.Imgg = ""
        for i in range(72):
            colorchoice = colorlist[tellerrow]
            self.i = Labeltest(i,i,"",colorchoice,tellerrow, tellercol)
            self.labelist[i]=self.i
            tellercol = tellercol + 1
            if tellercol >8:
                tellercol = 0
                tellerrow = tellerrow + 1
                
            
        Under=Frame(height = 30, width=600, bg = 'white')
        Under.grid(row = 9, column = 0, columnspan = 16, sticky = W)
        
        testButn =  Button(root,text ="placeImg")
        testButn.grid(row = 9, column = 0)
        testButn.bind("<Button-1>",lambda event,b = self.testturn:b())
    
    
    def testturn(self):
        number = random.randrange(0, 4)
        images = os.listdir("images")
        imagelist = ["Zia.jpg", "Ilja.jpg","Lynn.jpg","Philippe.jpg"]
        masterImg = Image.open(os.path.join("images",imagelist[number]))
            masterImg.thumbnail((40, 40))
            imge = ImageTk.PhotoImage(masterImg)
        self.Imgg = imge
        self.labelist[random.randrange(0, 72)].zconfig(self.Imgg)
        
class Labeltest:
    def __init__(self,naam,tekst,image,colorchoice,rij,kolomn):
        self.kleur = colorchoice
        self.tekst = tekst
        self.image = image
        self.rij=rij
        self.kolomn= kolomn
        self.naam = naam
        self.i=Frame(height = 50, width=50, bg = colorchoice, bd = 5,
relief= RIDGE)
        self.i.label=Label(text = self.tekst, bg = self.kleur)
        self.i.label.grid(row = self.rij, column = self.kolomn)
        self.i.grid(row = self.rij, column =  self.kolomn, sticky = NSEW)
        
    def zconfig(self,arg):
        self.i.label.config(image = arg)
root = Tk()
app = Bord(root)

root.mainloop()

The problem is that I want the images to stay on the labels when the button
testButn is pushed so that in the end most labels will contain an image.

With my code the image of the previously changed label is erased.
I can't figure out why, because I'm not changing the image value of that
label.

 So I'm stuck with this.

Any help would be greatly appreciated.

Philippe Smets