Tkinter Reloading Window displaying an Image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Kr
    New Member
    • Feb 2012
    • 6

    Tkinter Reloading Window displaying an Image

    Hi,
    I've been searching for this on google for a while, but I didn't find anything.
    I want to display an Image inside a Tkinter Window, but the Window should refresh the image every second or so.
    How do I do that?
    My Code so far:
    Code:
    import Tkinter 
    import Image, ImageTk
    
    im = Image.open('temp.png')
    
    root = Tkinter.Tk()
    tkimage = ImageTk.PhotoImage(im)
    
    Tkinter.Label(root, image=tkimage).pack()
    
    def update_image():
        im = None
        tkimage = None
        Tkinter.Label.destroy()
        im = Image.open('temp.png')
        tkimage = ImageTk.PhotoImage(im)
        Tkinter.Label(root, image=tkimage).pack()
        root.geometry('%dx%d' % (im.size[0],im.size[1]))
        root.after(1000, update_image)
    root.after(1000, update_image)
    root.mainloop()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You would open a bunch of images at the beginning of the program and place them into a list. The function would randomly select an image and display it. You should also save the id of the label so you can configure it in the function instead of creating a new label,
    Code:
    lab_1=Tkinter.Label(root, image=tkimage)
    lab_1.pack()
    
    def update_image():
        ...
        ##Tkinter.Label(root, image=tkimage).pack()
        ## replace with something like the following
        new_image=random.choice(image_list)
        lab_1.config(image=new_image)

    Comment

    • Peter Kr
      New Member
      • Feb 2012
      • 6

      #3
      My Problem is that I want to update the image, so if I would for example edit the image, it would change in the window with the next update.

      Comment

      • Peter Kr
        New Member
        • Feb 2012
        • 6

        #4
        Originally posted by dwblas
        You would open a bunch of images at the beginning of the program and place them into a list. The function would randomly select an image and display it. You should also save the id of the label so you can configure it in the function instead of creating a new label,
        Code:
        lab_1=Tkinter.Label(root, image=tkimage)
        lab_1.pack()
        
        def update_image():
            ...
            ##Tkinter.Label(root, image=tkimage).pack()
            ## replace with something like the following
            new_image=random.choice(image_list)
            lab_1.config(image=new_image)
        I changed the code to this:
        Code:
        import Tkinter 
        import Image, ImageTk
        im = Image.open('temp.png')
        
        def update_image():
            im = Image.open('temp.png')
            tkimage = ImageTk.PhotoImage(im)
            label.config(image=tkimage)
            root.geometry('%dx%d' % (im.size[0],im.size[1]))
            root.after(1000, update_image)
            
        root = Tkinter.Tk()
        im = Image.open('temp.png')
        tkimage = ImageTk.PhotoImage(im)
        label =  Tkinter.Label(root, image=tkimage)
        label.pack()
        root.after(1000, update_image)
        root.mainloop()
        but now, as soon as it updates, the window displays a grey background, but no picture. What'd I do wrong?
        Thanks for helping btw ;)

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          The variable tkimage is created in a function, which means it is local to the function and so is garbage collected when you exit the function, so no image to display. Keep a reference to it. You really should start learning classes as it makes keeping track of variables much easier.
          Code:
          def update_image():
               im = Image.open('temp.png')
          
               tkimage = ImageTk.PhotoImage(im)
               label.tkimage=tkimage     ## keep in instance of label
               # or just
               # label.tkimage = ImageTk.PhotoImage(im)
          
               label.config(image=label.tkimage)
               root.geometry('%dx%d' % (im.size[0],im.size[1]))
               root.after(1000, update_image)

          Comment

          • Peter Kr
            New Member
            • Feb 2012
            • 6

            #6
            Originally posted by dwblas
            The variable tkimage is created in a function, which means it is local to the function and so is garbage collected when you exit the function, so no image to display. Keep a reference to it. You really should start learning classes as it makes keeping track of variables much easier.
            Code:
            def update_image():
                 im = Image.open('temp.png')
            
                 tkimage = ImageTk.PhotoImage(im)
                 label.tkimage=tkimage     ## keep in instance of label
                 # or just
                 # label.tkimage = ImageTk.PhotoImage(im)
            
                 label.config(image=label.tkimage)
                 root.geometry('%dx%d' % (im.size[0],im.size[1]))
                 root.after(1000, update_image)
            Oh, right :/
            I am quite common with classes, but tkinter is like a whole new language to me xD
            Thanks, I'll try it as soon as possible :)

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              dwblas gave you good advice regarding classes. Another possibility is to create a reference to the image with the global keyword.
              Code:
              def update_image():
                  global tkimg1
                  tkimg1 = ImageTk.PhotoImage(Image.open(file_name))
                  w.config(image=tkimg1)
                  w.after(1000, update_image)

              Comment

              • Peter Kr
                New Member
                • Feb 2012
                • 6

                #8
                Thanks a lot both of you, works now!!!
                Code:
                import Tkinter 
                import Image, ImageTk
                
                def update_image():
                        global tkimg1
                        tkimg1 = ImageTk.PhotoImage(Image.open('temp.png'))
                        label.config( image = tkimg1)
                        label.after(1000, update_image)
                        print "Updated"
                
                
                    
                w = Tkinter.Tk()
                im = Image.open('temp.png')
                tkimg1 = ImageTk.PhotoImage(im)
                label =  Tkinter.Label(w, image=tkimg1)
                print "Loaded"
                label.pack()
                w.after(1000, update_image)
                w.mainloop()

                Comment

                Working...