[英]How to update the image of a Tkinter Label widget?
I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself.我希望能够换出 Tkinter 标签上的图像,但我不知道该怎么做,除了替换小部件本身。
Currently, I can display an image like so:目前,我可以这样显示图像:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
However, when the user hits, say the ENTER
key, I'd like to change the image.但是,当用户点击ENTER
键时,我想更改图像。
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
# change image
root.bind("<Return>", callback)
root.mainloop()
Is this possible?这可能吗?
The method label.configure
does work in panel.configure(image=img)
. label.configure
方法在panel.configure(image=img)
中有效。
What I forgot to do was include the panel.image=img
, to prevent garbage collection from deleting the image.我忘了做的是包括panel.image=img
,以防止垃圾收集删除图像。
The following is the new version:以下是新版本:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
img2 = ImageTk.PhotoImage(Image.open(path2))
panel.configure(image=img2)
panel.image = img2
root.bind("<Return>", callback)
root.mainloop()
The original code works because the image is stored in the global variable img
.原始代码有效,因为图像存储在全局变量img
中。
Another option to do it.另一种选择。
Using object-oriented programming and with an interactive interface to update the image.使用面向对象的编程并带有交互式界面来更新图像。
from Tkinter import *
import tkFileDialog
from tkFileDialog import askdirectory
from PIL import Image
class GUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
w,h = 650, 650
master.minsize(width=w, height=h)
master.maxsize(width=w, height=h)
self.pack()
self.file = Button(self, text='Browse', command=self.choose)
self.choose = Label(self, text="Choose file").pack()
self.image = PhotoImage(file='cualitativa.gif')
self.label = Label(image=self.image)
self.file.pack()
self.label.pack()
def choose(self):
ifile = tkFileDialog.askopenfile(parent=self,mode='rb',title='Choose a file')
path = ifile.name
self.image2 = PhotoImage(file=path)
self.label.configure(image=self.image2)
self.label.image=self.image2
root = Tk()
app = GUI(master=root)
app.mainloop()
root.destroy()
Replace 'cualitativa.jpg' for the default image you want to use.将“cualitativa.jpg”替换为您要使用的默认图像。
Another solution that might be of help.另一个可能有帮助的解决方案。
In my case, I had two tk.Tk()
windows.就我而言,我有两个tk.Tk()
窗口。 When using ImageTk.PhotoImage()
, the object defaults to setting its tk window to being the first one created.使用ImageTk.PhotoImage()
时,对象默认将其 tk 窗口设置为第一个创建的窗口。 A simple fix to this is to pass the tk window that you want as such ImageTk.PhotoImage(img, master=your_window)
一个简单的解决方法是传递您想要的 tk 窗口,如ImageTk.PhotoImage(img, master=your_window)
import tkinter as tk
from PIL import ImageTk, Image
if __name__ == '__main__':
main_window = tk.Tk()
second_window = tk.Tk()
main_canvas = Canvas(second_window)
main_canvas.pack()
filename = 'test.png'
img = Image.open(filename)
img = img.resize((300, 100), Image.ANTIALIAS)
logo = ImageTk.PhotoImage(img, master=second_window)
logo_label = Label(master=main_canvas, image=logo)
logo_label.image = logo
logo_label.pack()
main_window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.