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: -
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 gave you good advice regarding classes. Another possibility is to create a reference to the image with the global keyword. - def update_image():
-
global tkimg1
-
tkimg1 = ImageTk.PhotoImage(Image.open(file_name))
-
w.config(image=tkimg1)
-
w.after(1000, update_image)
7 17398
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, - 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)
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.
@dwblas
I changed the code to this: -
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 ;)
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. -
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)
@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 :)
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. - def update_image():
-
global tkimg1
-
tkimg1 = ImageTk.PhotoImage(Image.open(file_name))
-
w.config(image=tkimg1)
-
w.after(1000, update_image)
Thanks a lot both of you, works now!!! - 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()
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:')...
|
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..
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |