2016-05-17 13 views
0

Python 2.7のTkinterモジュールで簡単なUIを作成しようとしていますが、画像をブラウズして選択してUIに表示するのですが、画像をブラウズして選択すると読み込んだ画像)がクリアされますが、選択した画像は画面に表示されません。画像がPythonで表示されないUIのTkinter

from Tkinter import * 
from PIL import ImageTk, Image 
import tkFileDialog 

root = Tk() 
root.title('Simple Image Display app') 

w = Canvas(root, width=1100, height=600) 
w.pack() 
root.resizable(width=FALSE, height=FALSE) 

img = ImageTk.PhotoImage(Image.open('test_2.JPG').convert('LA')) 
panel = Label(root, image = img) 
panel.place(x=700,y=100) 


def Open():  ## function to open file dialog and select the file to use in the reader application 
    dialog = Tk() 
    dialog.withdraw() 
    fname = tkFileDialog.askopenfilename(filetypes = (("Image Files", "*.JPG"), ("All files", "*"))) 
    dialog.destroy() 
    img = ImageTk.PhotoImage(Image.open(fname).convert('LA')) 
    panel.configure(image = img) 



menubar = Menu(root) 

# File Menu 
filemenu = Menu(menubar, tearoff=0) 
filemenu.add_command(label="Open", command=Open) 
menubar.add_cascade(label="File", menu=filemenu) 


# display the menu 
root.config(menu=menubar) 
root.mainloop() 

問題を解決するための助力や提案はありますか?

ありがとうございます。

+0

コンソールにエラーはありますか? –

答えて

0

前に誰かがSOにこの問題を持っているように見えます:それは適用範囲外ですので、それが画像上garbage collectionをやっTkinterに沸くHow to update the image of a Tkinter Label widget?

panel.configure(...)行の下にpanel.image = imgを追加します。

0

@ nicholas-smithからの変更が提案された後、私のコードは正常に動作しています。 はここで更新され、作業コード

def Open():  ## function to open file dialog and select the file to use in the reader application 
    dialog = Tk() 
    dialog.withdraw() 
    fname = tkFileDialog.askopenfilename(filetypes = (("Image Files", "*.JPG"), ("All files", "*"))) 
    dialog.destroy() 
    img2 = ImageTk.PhotoImage(Image.open(fname).convert('LA')) 
    panel.configure(image = img2) 
    panel.image = img2 

だけ「オープン」機能を変更する必要があるのpython 2.7のために。

関連する問題