在<_io中恢复路径。缓冲读取器名称='路径'>浏览文件后 Tkinter



我正在使用tkinter Python库创建一个应用程序,并且已经有这个:

class Application(Tk) :
    def __init__(self):
        Tk.__init__(self)
        self.Launch = Button(self, text="Launch", command=self.launchCallBack)
        self.Browse = Button(self, text="Browse", command=self.browseCallBack)
        self.pathlabel = Label(self)
        self.file = ''
        self.Launch.pack()
        self.Browse.pack()
        self.pathlabel.pack()
    def browseCallBack(self) :
        self.file = filedialog.askopenfile(parent=self, mode='rb', title='Choose a file', initialdir = "D:\UsersT0211254MyAppBundle CUD-CAPELLA 431melodyeclipseworkspace", filetypes=[("aird Files", "*.aird")])
        self.pathlabel.config(text=str(self.file))
    def launchCallBack(self):
        create_file(self.file)

问题是我的 self.file 属性返回我:

<_io.BufferedReader name='MyFilePath'>

我只想恢复MyFilePath.

感谢您的帮助!

该名称在 name 属性中的 BufferedReader s 上可用,因此self.file.name会得到您想要的。

但是,您可能希望改用filedialog.askopenfilename(),以便仅获取名称,而不是打开的文件对象。

相关内容

最新更新