473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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:
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)

7 17398
dwblas
626 Expert 512MB
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
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
@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
dwblas
626 Expert 512MB
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
@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
2,851 Expert Mod 2GB
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
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

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

Similar topics

5
by: Peter Moscatt | last post by:
Ok.... I am pretty new to Python (as you may have gathered from previous posts). So now it time for another one of my ridiculous questions.... :-) When using 'Tkinter', what is used as the...
2
by: Svennglenn | last post by:
How do i make a child window "active" like the root window? from Tkinter import * def open_child(): c = Toplevel(root) c.title("Child window") c.geometry('200x160+230+130') Label(c,...
3
by: Tuvas | last post by:
Is there a way to make a new window pop up using Tkinter? I have some functions that require more data in my interface than I have space for, and would like to be able to pop up a new window to...
3
by: Irfan Akram | last post by:
Hi Guys, I am having problems displaying an image from the database, that alreday has been uoploaded successfully. The image loads in correctly, but by default it is displayed on the whole...
1
by: aabruzzese | last post by:
Hi folks, Been struggling with this little puzzle. I am reading the MemberInfo table for the ClubStarterKit, the table includes the Avatar for the User. I would like to display the Avatar...
3
by: CD | last post by:
An application is logging faxes sent in SQL2000 image column type. I have found code on the net but what it is doing is prompting to save to local which is fine for single page image. Not good...
2
by: sj | last post by:
I am just learning to use Tkinter and am having problems displaying image files. I am able to display an image using tutorials (such as http://www.daniweb.com/code/snippet296.html) But when I try...
8
rhitam30111985
by: rhitam30111985 | last post by:
hi all.. python noob here.. i am creating a GUI for an applcation... now i was just testing the working of askstring : from tkSimpleDialog import askstring string=askstring('site','enter site:')...
4
by: honey99 | last post by:
hi! can anybody give me some example code...for how to upload an image file in a JSP page and display that image in the same JSP without reloading/refreshing the page????plzzzzz help me..
6
by: Jeff | last post by:
hi asp.net 2.0 I have a image (.jpeg) stored in sql server 2005 and now I want to display it on a webpage. So I created a webpage (Image.aspx) which just writes the buffer data to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.