如何选择一个目录,并使用Python中Tkinter的存储位置 [英] How to select a directory and store the location using tkinter in Python

查看:2466
本文介绍了如何选择一个目录,并使用Python中Tkinter的存储位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个浏览按钮,我只希望返回路径的GUI。我一直在寻找使用code像下面的解决方案。

I am creating a GUI with a browse button which I only want to return the path. I've been looking at solutions using code like below.

Tkinter.Button(subframe, text = "Browse", command = self.loadtemplate, width = 10).pack()

   def loadtemplate(self): 
        filename = tkFileDialog.askopenfilename(filetypes = (("Template files", "*.tplate")
                                                             ,("HTML files", "*.html;*.htm")
                                                             ,("All files", "*.*") ))
        if filename: 
            try: 
                self.settings["template"].set(filename)
            except: 
                tkMessageBox.showerror("Open Source File", "Failed to read file \n'%s'"%filename)

不过,我知道Tkinter的有一个内置的 askopenfilename 这是打开文件code的一个超级简单的一条线。是否有某种方式来修改这个返回目录而不是一个文件?难道还有比code我张贴?

However I know Tkinter has a built in askopenfilename which is a super easy one line of code for opening files. Is there some way to modify this to return the directory instead of a file? Is there a smaller option than the larger chunk of code I posted?

推荐答案

看来, tkFileDialog.askdirectory 应该工作。 文档

这篇关于如何选择一个目录,并使用Python中Tkinter的存储位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆