【发布时间】:2015-05-01 15:47:50
【问题描述】:
我正在尝试使用 tkinter filedialog 在我的 Python 3.4 程序中获取用户对文件的选择。
以前,我尝试使用 Gtk FileChooserDialog,但我不断地碰壁,让它工作 (here's my question about that.) 所以,我(尝试)切换到 tkinter并使用文件对话框。
这是我用于 GUI 的代码:
import tkinter
from tkinter import filedialog
root = tkinter.Tk()
root.withdraw()
path = filedialog.askopenfile()
print(type(path)) # <- Not actually in the code, but I've included it to show the type
它工作得很好,除了它返回一个<class '_io.TextIOWrapper'> 对象而不是一个字符串,就像我预期/需要的那样。
调用str() 不起作用,使用io 模块函数getvalue() 也不起作用。
有谁知道我如何从filedialog.askopenfile() 函数中获取所选文件路径作为字符串?
【问题讨论】:
标签: python user-interface tkinter