我想在文本框中打印文件路径。我刚刚创建了一个文本框和一个按钮,用于调用“选择文件”窗口。现在我必须打印选定的文件路径。
我的代码是:

import Tkinter, tkFileDialog, Tkconstants
from Tkinter import *

def open():
    File = tkFileDialog.askopenfile(parent=root,mode='r',title='Choose a file')
    for f in File:
        yourName.insert(1.0, f.read())

root = Tk()
custName = StringVar(None)
yourName = Entry(root, textvariable=custName)
yourName.grid(column=0,row=0,sticky='EW')
yourName.update()
yourName.focus_set()
yourName.pack(padx = 20, pady = 20,anchor='n')
yourName.place(y = 25, x = 100, width = 525, height = 25)

button = Button(root, text='Take a Picture',command = open)
button.pack(padx = 1, pady = 1,anchor='ne')
button.place( x = 650, y = 25)

root.mainloop()

欢迎提出任何建议!

最佳答案

如果需要选定文件的路径,请使用askopenfilename而不是askopenfile

def open():
    filename = tkFileDialog.askopenfilename(parent=root,title='Choose a file')
    custName.set(filename)

10-06 01:01