如何使用tkinter在文本框中打印文件路径

时间:2014-03-31 18:42:34

标签: python user-interface tkinter

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

我的代码是:

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()

欢迎任何建议!

1 个答案:

答案 0 :(得分:6)

如果您想要所选文件的路径,请使用askopenfilename代替askopenfile

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