如何更新 tkinter 标签中的图像? [英] How to update image in tkinter label?

查看:38
本文介绍了如何更新 tkinter 标签中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 的初学者,所以这可能太简单了,但我需要帮助.使用此代码,我无法更新 tkinter 标签中的图像.我什至可以根据新加载图像的属性调整窗口大小,但新图像未显示在 tkinter 标签中.

I'm a beginner in python so this may be too simple question to ask but i need help..With this code i cannot update image in tkinter label. I can even resize window according to new loaded image's attributes but the new image is not displayed in tkinter label.

from Tkinter import Frame, Tk, Label, Text, Menu, END, BOTH, StringVar
from PIL import ImageTk, Image
import numpy
import tkFileDialog

class DIP(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent) 
        self.parent = parent        
        self.initUI()

def initUI(self):

    self.parent.title("DIP Algorithms- Simple Photo Editor")
    self.pack(fill=BOTH, expand=1)

    menubar = Menu(self.parent)
    self.parent.config(menu=menubar)

    #Open Image Menu
    fileMenu = Menu(menubar)
    fileMenu.add_command(label="Open", command=self.onOpen)
    menubar.add_cascade(label="File", menu=fileMenu)

    #menu for image ngative
    basicMenu=Menu(menubar)
    basicMenu.add_command(label="Negative", command=self.onNeg)
    menubar.add_cascade(label="Basic", menu=basicMenu)

#Image Negative Menu callback
def onNeg(self):
    I2=255-self.I;
    im = Image.fromarray(numpy.uint8(I2))
    photo2=ImageTk.PhotoImage(im)
    self.label2= Label(self.parent,border=25,image=photo2)
    self.label2.image = photo2 # keep a reference!
    self.label2.grid(row=1, column=2)


def setImage(self):

    self.img=Image.open(self.fn)
    self.I = numpy.asarray(self.img)
    l,h = self.img.size
    text=str(2*l+100)+"x"+str(h+50)+"+0+0"
    self.parent.geometry(text)
    photo = ImageTk.PhotoImage(self.img)
    self.label1 = Label(self.parent,border=25,image=photo)
    self.label1.configure(image=photo)
    self.label1.image = photo # keep a reference!
    self.label1.grid(row=1, column=1)

#Open Callback
def onOpen(self):

    ftypes = [('Image Files', '*.tif *.jpg *.png')]
    dlg = tkFileDialog.Open(self, filetypes = ftypes)
    filename = dlg.show()
    self.fn=filename
    #print self.fn #prints filename with path here
    self.setImage()

#def onError(self):
    #box.showerror("Error", "Could not open file")    

def main():

    root = Tk()
    DIP(root)
    root.geometry("320x240")
    root.mainloop()  


if __name__ == '__main__':
    main()

当我运行此代码并打开图像时,它会显示在 label1 中.但是当我再次打开另一个图像时,我希望它显示在同一个 label1 中,但它没有发生.我知道第二张图像已加载,因为窗口大小相应地调整了大小,唯一的问题是它没有显示,我不知道为什么!.

When i run this code, and open an image , it is displayed in label1. But when i open another image again, i' expecting it to be displayed in same label1, but it's not happening. I know the 2nd image is loaded because the window size resized accordingly, the only problem is that it's not being displayed and i cannot figure out why!.

推荐答案

不是每次调用 setImage 时都创建一个新的 tk.Label,只需在外面创建一次setImage -- 例如,在 initUI.

Instead of creating an new tk.Label each time setImage is called, just create it once outside of setImage -- for example, in initUI.

然后您可以通过调用self.label.configure来更改图像:

You can then change the image by calling self.label.configure:

import Tkinter as tk
import Image
import ImageTk
import numpy as np
import tkFileDialog

class DIP(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent) 
        self.parent = parent        
        self.initUI()

    def initUI(self):
        self.parent.title("DIP Algorithms- Simple Photo Editor")
        self.pack(fill = tk.BOTH, expand = 1)

        menubar = tk.Menu(self.parent)
        self.parent.config(menu = menubar)

        self.label1 = tk.Label(self, border = 25)
        self.label2 = tk.Label(self, border = 25)
        self.label1.grid(row = 1, column = 1)
        self.label2.grid(row = 1, column = 2)

        #Open Image Menu
        fileMenu = tk.Menu(menubar)
        fileMenu.add_command(label = "Open", command = self.onOpen)
        menubar.add_cascade(label = "File", menu = fileMenu)

        #menu for image ngative
        basicMenu = tk.Menu(menubar)
        basicMenu.add_command(label = "Negative", command = self.onNeg)
        menubar.add_cascade(label = "Basic", menu = basicMenu)

    def onNeg(self):
        #Image Negative Menu callback
        I2 = 255-self.I;
        im = Image.fromarray(np.uint8(I2))
        photo2 = ImageTk.PhotoImage(im)
        self.label2.image = photo2 # keep a reference!

    def setImage(self):
        self.img = Image.open(self.fn)
        self.I = np.asarray(self.img)
        l, h = self.img.size
        text = str(2*l+100)+"x"+str(h+50)+"+0+0"
        self.parent.geometry(text)
        photo = ImageTk.PhotoImage(self.img)
        self.label1.configure(image = photo)
        self.label1.image = photo # keep a reference!

    def onOpen(self):
        #Open Callback
        ftypes = [('Image Files', '*.tif *.jpg *.png')]
        dlg = tkFileDialog.Open(self, filetypes = ftypes)
        filename = dlg.show()
        self.fn = filename
        #print self.fn #prints filename with path here
        self.setImage()

    #def onError(self):
        #box.showerror("Error", "Could not open file")    

def main():

    root = tk.Tk()
    DIP(root)
    root.geometry("320x240")
    root.mainloop()  


if __name__ == '__main__':
    main()

这篇关于如何更新 tkinter 标签中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆