2013-01-12 51 views
1

我是一個Python初學者,所以這可能是太簡單的問題,但我需要幫助。有了這段代碼,我無法更新tkinter標籤中的圖像。我甚至可以根據新加載的圖像的屬性調整窗口大小,但新圖像不會顯示在tkinter標籤中。如何更新tkinter標籤中的圖像?

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中。但是當我再次打開另一個圖像時,我期待它顯示在同一個標​​籤1中,但它沒有發生。我知道第二個圖像被加載,因爲窗口大小相應地調整大小,唯一的問題是它沒有被顯示,我不明白爲什麼!

回答

2

而不是創建一個新的tk.Label每次調用setImage時,只需在setImage之外創建一次 - 例如在initUI中。

然後,您可以通過調用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() 
+0

@unubtu'self.label1.image =照片#保留引用'爲什麼是保持一個重要參考!? – Nancy

+0

@Nancy:好問題。請參閱[爲什麼您必須保留對PhotoImages的引用](http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm) – unutbu