By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,790 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,790 IT Pros & Developers. It's quick & easy.

Tkinter Reloading Window displaying an Image

P: 6
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:
Expand|Select|Wrap|Line Numbers
  1. import Tkinter 
  2. import Image, ImageTk
  3.  
  4. im = Image.open('temp.png')
  5.  
  6. root = Tkinter.Tk()
  7. tkimage = ImageTk.PhotoImage(im)
  8.  
  9. Tkinter.Label(root, image=tkimage).pack()
  10.  
  11. def update_image():
  12.     im = None
  13.     tkimage = None
  14.     Tkinter.Label.destroy()
  15.     im = Image.open('temp.png')
  16.     tkimage = ImageTk.PhotoImage(im)
  17.     Tkinter.Label(root, image=tkimage).pack()
  18.     root.geometry('%dx%d' % (im.size[0],im.size[1]))
  19.     root.after(1000, update_image)
  20. root.after(1000, update_image)
  21. root.mainloop()
  22.  
  23.  
Feb 28 '12 #1

✓ answered by bvdet

dwblas gave you good advice regarding classes. Another possibility is to create a reference to the image with the global keyword.
Expand|Select|Wrap|Line Numbers
  1. def update_image():
  2.     global tkimg1
  3.     tkimg1 = ImageTk.PhotoImage(Image.open(file_name))
  4.     w.config(image=tkimg1)
  5.     w.after(1000, update_image)

Share this Question
Share on Google+
7 Replies

Expert 100+
P: 626
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,
Expand|Select|Wrap|Line Numbers
  1. lab_1=Tkinter.Label(root, image=tkimage)
  2. lab_1.pack()
  3.  
  4. def update_image():
  5.     ...
  6.     ##Tkinter.Label(root, image=tkimage).pack()
  7.     ## replace with something like the following
  8.     new_image=random.choice(image_list)
  9.     lab_1.config(image=new_image) 
Feb 28 '12 #2

P: 6
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.
Feb 29 '12 #3

P: 6
@dwblas
I changed the code to this:
Expand|Select|Wrap|Line Numbers
  1. import Tkinter 
  2. import Image, ImageTk
  3. im = Image.open('temp.png')
  4.  
  5. def update_image():
  6.     im = Image.open('temp.png')
  7.     tkimage = ImageTk.PhotoImage(im)
  8.     label.config(image=tkimage)
  9.     root.geometry('%dx%d' % (im.size[0],im.size[1]))
  10.     root.after(1000, update_image)
  11.  
  12. root = Tkinter.Tk()
  13. im = Image.open('temp.png')
  14. tkimage = ImageTk.PhotoImage(im)
  15. label =  Tkinter.Label(root, image=tkimage)
  16. label.pack()
  17. root.after(1000, update_image)
  18. root.mainloop()
  19.  
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 ;)
Feb 29 '12 #4

Expert 100+
P: 626
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.
Expand|Select|Wrap|Line Numbers
  1. def update_image():
  2.      im = Image.open('temp.png')
  3.  
  4.      tkimage = ImageTk.PhotoImage(im)
  5.      label.tkimage=tkimage     ## keep in instance of label
  6.      # or just
  7.      # label.tkimage = ImageTk.PhotoImage(im)
  8.  
  9.      label.config(image=label.tkimage)
  10.      root.geometry('%dx%d' % (im.size[0],im.size[1]))
  11.      root.after(1000, update_image) 
Feb 29 '12 #5

P: 6
@dwblas
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 :)
Feb 29 '12 #6

bvdet
Expert Mod 2.5K+
P: 2,851
dwblas gave you good advice regarding classes. Another possibility is to create a reference to the image with the global keyword.
Expand|Select|Wrap|Line Numbers
  1. def update_image():
  2.     global tkimg1
  3.     tkimg1 = ImageTk.PhotoImage(Image.open(file_name))
  4.     w.config(image=tkimg1)
  5.     w.after(1000, update_image)
Feb 29 '12 #7

P: 6
Thanks a lot both of you, works now!!!
Expand|Select|Wrap|Line Numbers
  1. import Tkinter 
  2. import Image, ImageTk
  3.  
  4. def update_image():
  5.         global tkimg1
  6.         tkimg1 = ImageTk.PhotoImage(Image.open('temp.png'))
  7.         label.config( image = tkimg1)
  8.         label.after(1000, update_image)
  9.         print "Updated"
  10.  
  11.  
  12.  
  13. w = Tkinter.Tk()
  14. im = Image.open('temp.png')
  15. tkimg1 = ImageTk.PhotoImage(im)
  16. label =  Tkinter.Label(w, image=tkimg1)
  17. print "Loaded"
  18. label.pack()
  19. w.after(1000, update_image)
  20. w.mainloop()
  21.  
Mar 4 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.