0

I'm trying to create a loop of buttons in Tk while each of the buttons have image embedded to it, when I first tried the loop without images it worked but now when I add images it doesn't

def menu_window(self, box):
        """Specific function for opening the menu window"""
        window = Toplevel()
        self.activate_window(window, 420, 380)  # window default activation at size 400x400
        menu = self.waiter.full_menu()
        SoupMaker(self.main_url+FOOD_IMAGES).get_images()  # Download menu images.
        counter, rowc = 0, 0
        for item in menu:  # Create a button for every item in the website menu - on click, insert item to order list.
            food_img = ImageTk.PhotoImage(Image.open(item+'.png'))
            bt = Button(window, image=food_img)  # on click
            if counter >= 3 and counter % 3 == 0:
                counter = 0
                rowc += 1
            bt.configure(command=lambda i=item + ' ' + menu[item]: box.insert(END, i))
            bt.grid(sticky='W', row=counter, column=rowc)  # increase row number for every button
            counter += 1
        print 'done buttons'
        window.mainloop()

While the buttons do show in the window, they are empty and not usable(Configured commands don't work)... This is an image of the screen with the empty images:

IMG LINK

  • Please create a [mcve]. Also, you probably need to remove the call to `mainloop`. A normal tkinter program needs to call that exactly once for the life of the program. – Bryan Oakley Apr 05 '18 at 15:25
  • The problem is probably this: https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function – Bryan Oakley Apr 05 '18 at 15:26
  • 1
    My window is supposed to keep up though, and thanks but I found a solution which was adding bt.image = food_image which should save the reference – Lior Shemaya Apr 05 '18 at 16:00
  • I don't know what you mean by "keep up", but having more than one call to `mainloop()` won't make things better. – Bryan Oakley Apr 05 '18 at 16:09
  • Why wouldn't it? I want 2 main windows active at the same time.. I have a main window and menu window which both of them are suppose to be active together – Lior Shemaya Apr 05 '18 at 16:14
  • Because you simply don't need two. A single call to `mainloop` is global, and works for all widgets that are children of the same root window. `mainloop` is the _main_ loop, not a _window_ loop. – Bryan Oakley Apr 05 '18 at 16:40
  • Thanks for the helpful information! I fixed it in my code now. – Lior Shemaya Apr 05 '18 at 19:28

1 Answers1

0

The problem could be the fact that the interface is not updated while the program is running

class GUI:
    def __init__(self,main):
        loop
            main.update()
root=Tk()
GUI(root)
root.mainloop()

if your "window" is the main of tkinter you can use in the loop window.update()