Python tkinter: Replacing an image button with an image label
Question: Hi I'm trying to make a code that would replace an image button with an image
label when the button is pressed. But the window isn't updating so the new
image doesn't became visible. Can anybody help me? If it is even possible to
do that way.

There is the code I'm trying:

    
    
    from tkinter import *
    import time
    
    gifdir = "./"
    class Game:
       def __init__(self):
          self.__window = Tk()
    
    
    
          igm = PhotoImage(file=gifdir+"empty.gif")
    
          Button(self.__window, image=igm, command= self.change_picture)\
          .grid(row=1, column=2, sticky=E)
    
          def change_picture():
            igm = PhotoImage(file=gifdir+"new.gif")
            Label(self.__window, image=igm,)\
            .grid(row=1, column=2, sticky=E)
    
           self.__window.mainloop()
    
    def main():
        Game()
    
    
    main()
    

When I add this code to the end:

    
    
       self.__window.update_idletasks()
       time.sleep(1)
    

the new picture is shown for a one second but I need to see it all the time
and still be able to press other buttons.


Answer: I modified your code, as your code is very strangely designed, and incorrect
IMO. This is the modified version:

    
    
    from tkinter import *
    
    import time
    
    class Game:
        def __init__(self):
            self.__window = Tk()
    
            self.gifdir = "./"
    
            self.igm = PhotoImage(file=self.gifdir+"empty.gif")
    
            self.btn = Button(self.__window, image=self.igm, command = self.change_picture)
            self.btn.grid(row=1, column=2, sticky=E)
    
            self.__window.mainloop()                
    
        def change_picture(self):
            self.igm = PhotoImage(file=self.gifdir+"new.gif")
            self.btn.configure(image = self.igm)              
    
    
    def main():
        Game()
    
    
    main()
    

In this new version, pressing the button, will change the image on it.
Basically, in your class, you need to keep references to created widgets.
Especially keeping a reference for `PhotoImage` is important, as if the
reference is not kept, garbage collector will remove the image, when instance
of `PhotoImage` will go out of scope in `change_picture`.

