python存储路径_如何使用tkinter在Python中选择目录并存储位置

1586010002-jmsa.png

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)

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?

解决方案

It appears that tkFileDialog.askdirectory should work. documentation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值