Python 如何更新Tkinter标签小部件的图像?
我希望能够交换出Tkinter标签上的图像,但我不知道如何做,除了更换小部件本身 目前,我可以显示如下图像:Python 如何更新Tkinter标签小部件的图像?,python,python-2.7,tkinter,python-imaging-library,Python,Python 2.7,Tkinter,Python Imaging Library,我希望能够交换出Tkinter标签上的图像,但我不知道如何做,除了更换小部件本身 目前,我可以显示如下图像: 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.
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()
但是,当用户点击时,比如说按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()
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()
将Tkinter作为tk导入
导入ImageTk
root=tk.tk()
img=ImageTk.PhotoImage(Image.open(path))
panel=tk.Label(根,图像=img)
panel.pack(side=“bottom”,fill=“both”,expand=“yes”)
def回调(e):
#改变形象
root.bind(“,回调)
root.mainloop()
这可能吗?方法
标签。配置在面板中起作用。配置(image=img)
我忘了包括面板。image=img
,以防止垃圾收集删除图像
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()
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()
新版本如下:
将Tkinter作为tk导入
导入ImageTk
root=tk.tk()
img=ImageTk.PhotoImage(Image.open(path))
panel=tk.Label(根,图像=img)
panel.pack(side=“bottom”,fill=“both”,expand=“yes”)
def回调(e):
img2=ImageTk.PhotoImage(Image.open(路径2))
panel.configure(image=img2)
panel.image=img2
root.bind(“,回调)
root.mainloop()
原始代码之所以有效,是因为图像存储在全局变量img
中,这是另一个执行此操作的选项
使用面向对象编程,并具有交互式界面来更新图像
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()
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()
替换要使用的默认图像的“cualitiva.jpg” 另一个可能有用的解决方案
在我的例子中,我有两个tk.tk()
windows。使用ImageTk.PhotoImage()
时,对象默认将其tk窗口设置为第一个创建的窗口。解决这个问题的一个简单方法是传递您想要的tk窗口ImageTk.PhotoImage(img,master=your_窗口)
@这似乎是有道理的。我不记得特别需要修改这个用法,但这也是四年前的事了。你能测试一下以确认吗?是的,这可能就是问题所在。使用略有不同的代码进行测试,但问题相同。看来@Axl删除了出现此问题的原因。@skeggse我删除了它,因为我认为根本没有必要。当有人在寻找同一个问题的解决方案时,他/她不在乎是谁提出的或不是什么,他/她只是想要一个具体的答案。是的,但对我来说,让人觉得我已经想出了一个解决方案是不公平的,因为我不是真正想出解决方案的人。