当前位置:主页 > 软件编程 > Python代码 >

Python3 Tkinter选择路径功能的实现方法

时间:2021-07-12 13:29:53 | 栏目:Python代码 | 点击:

效果基于Python3。

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

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

效果预览

这是选择前:

选择:

选择后:

代码

很基础的写法。

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()方法是返回文件夹路径不是文件路径。

您可能感兴趣的文章:

相关文章