-1

I'm trying to make a program in Python 3.3.0 to train with Tkinter, but when I try to put images on buttons which are created in a loop, I obtain a few buttons that don't work (I can't click on them and they don't have images), and the last one is working and with the image on it. Here there's the code:

elenco = [immagine1, immagine2, immagine3, immagine 4]
class secondWindow:

    def __init__(self):

        self.secondWindow = Tk()
        self.secondWindow.geometry ('500x650+400+30')

class mainWindow:

    def __init__(self):

        self.mainWindow = Tk()
        self.mainWindow.geometry ('1100x650+100+10')
        self.mainWindow.title('MainWindow')

    def Buttons(self, stringa):

        i = 0
        for _ in elenco:
            if stringa in _.lower():
                j = int(i/10)
                self.IM = PIL.Image.open (_ + ".jpg")
                self.II = PIL.ImageTk.PhotoImage (self.IM)             
                self.button = Button(text = _, compound = 'top', image =  self.II, command = secondWindow).grid(row = j, column = i-j*10)
                i += 1

    def mainEnter (self):

        testoEntry = StringVar()      
        self.mainEntry = Entry(self.mainWindow, textvariable = testoEntry).place (x = 900, y = 20)

        def search ():
            testoEntry2 = testoEntry.get()
            if testoEntry2 == "":
                pass
            else:
                testoEntry2 = testoEntry2.lower()
            mainWindow.Buttons(self, testoEntry2)

        self.button4Entry = Button (self.mainWindow, text = 'search', command = search).place (x = 1050, y = 17)


MW = mainWindow()
MW.mainEnter()
mainloop()

If I try to create buttons in a loop without images, they work:

def Buttons(self, stringa):

    i = 0

    for _ in elenco:
        if stringa in _.lower():
            j = int(i/10)
            self.button = Button(text = _, command = secondWindow).grid(row = j, column = i-j*10)
            i += 1

And if I try to create a button with an image but not in a loop, it works too:

im = PIL.Image.open("immagine1.jpg")
ge = PIL.ImageTk.PhotoImage (im)
butt = Button(text = 'immagine', compound = 'top', image = ge, command = secondWindow).grid(row = 0, column = 0)
vladi
  • 48
  • 5
  • 1
    You're only keeping a reference to the *last* image, and the last button. Use lists instead (`self.buttons.append(...)`). – jonrsharpe Jul 14 '16 at 10:18
  • @jonrsharpe I've tried with dictionaries and it dosn't work, I'll try with a list... – vladi Jul 14 '16 at 10:21
  • 2
    Additionally, you're storing the result of `.grid`, which is actually `None`, not the button itself. – jonrsharpe Jul 14 '16 at 10:22
  • I've tried also with lists... And I don't know what to do... Maybe it is a stupid thing to do, but I've just started with Tkinter and I'm not an expert with Python – vladi Jul 14 '16 at 11:25
  • Well then please give a [mcve] illustrating your problem. – jonrsharpe Jul 14 '16 at 11:26
  • I've posted the code... – vladi Jul 14 '16 at 11:30
  • 1
    You've posted your old code, and I've told you why that won't work; you need to keep references to *all images*. Please give a [mcve] of your attempt to fix this with lists. – jonrsharpe Jul 14 '16 at 12:01
  • Possible duplicate of [Having trouble creating buttons with a for-loop through tkinter. (Python)](http://stackoverflow.com/questions/33123004/having-trouble-creating-buttons-with-a-for-loop-through-tkinter-python) – J.J. Hakala Jul 14 '16 at 19:01

1 Answers1

0

Let's say you have images named "image-i.png", i=0,..,9. When I execute the following code (with python 3.5) I obtain ten working buttons with image and text:

from tkinter import Tk, Button, PhotoImage

root = Tk()

images = []
buttons = []

for  i in range(10):
    im = PhotoImage(master=root, file="image-%i.png" % i)

    b = Button(root, text="Button %i" % i, image=im, compound="left", 
               command=lambda x=i: print(x))
    b.grid(row=i)

    images.append(im)
    buttons.append(b)

root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • Ah ok... I'll try to code in this way... My error was that I was making lists with only buttons or only images... Forgot to do both one thing and another... Thanks – vladi Jul 14 '16 at 13:48