Tkinter filedialog askopenfilename函数

问题描述 投票:0回答:1

[当用户通过调用函数file dialog按下按钮时,我正在尝试打开open(选择文件)窗口:

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

root = Tk()
root.title('Application')


def open_file():
    root.filename = tkFileDialog.askopenfilename(initialdir="/", title="Select An Image", filetypes=(("jpeg files", "*.jpg"), ("gif files", "*.gif*"), ("png files", "*.png")))
    image_label = Label(root, text=root.filename)
    image_label.pack()
    my_image = ImageTk.PhotoImage(Image.open(root.filename))
    my_image_label = Label(root, image=my_image)
    my_image_label.pack()

my_button = Button(root, text="Open File", command=open_file)
my_button.pack()
root.mainloop()

但是,在我选择了选定的文件并“提交”之后,它不会出现在我创建的my_image_label上(仅出现图片大小的空白)但是,当我在函数外部(不调用函数)使用open函数内容时,它可以工作。

您知道什么是问题所在吗?以及如何解决?

tkinter python-imaging-library openfiledialog photoimage getopenfilename
1个回答
0
投票

我没有安装2.7,所以这是我的最佳猜测:root.filename应该只是我认为的文件名。

print root.filename返回什么?

© www.soinside.com 2019 - 2024. All rights reserved.