Python 如何更新 Tkinter Label 小部件的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3482081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:17:33  来源:igfitidea点击:

How to update the image of a Tkinter Label widget?

pythonpython-2.7tkinterpython-imaging-library

提问by skeggse

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 ENTERkey, 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?

这可能吗?

采纳答案by skeggse

The method label.configuredoes work in panel.configure(image=img).

该方法label.configurepanel.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

回答by Daniel Santos

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.

将 'culitativa.jpg' 替换为您要使用的默认图像。