[当用户通过调用函数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
函数内容时,它可以工作。
您知道什么是问题所在吗?以及如何解决?
我没有安装2.7,所以这是我的最佳猜测:root.filename
应该只是我认为的文件名。
print root.filename
返回什么?