效果基于Python3。

在自己写小工具的时候因为这个功能纠结了一会儿,这里写个小例子,供有需要的参考。

小例子,就是点击按钮打开路径选择窗口,选择后把值传给Entry输出。

效果预览

这是选择前:

python选择路径 python选择文件路径_python选择路径

选择:

python选择路径 python选择文件路径_值传_02

选择后:

python选择路径 python选择文件路径_值传_03

代码

很基础的写法。

from tkinter import *
from tkinter.filedialog import askdirectory
def selectPath():
path_ = askdirectory()
path.set(path_)
root = Tk()
path = StringVar()
Label(root,text = "目标路径:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "路径选择", command = selectPath).grid(row = 0, column = 2)
root.mainloop()

注意事项

1.注意import模块时的写法。

2.askdirectory()方法是返回文件夹路径不是文件路径。

以上这篇Python3 Tkinter选择路径功能的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。