脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python3 Tkinter选择路径功能的实现方法

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

2021-07-12 00:32Clew123 Python

今天小编就为大家分享一篇Python3 Tkinter选择路径功能的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

效果基于python3。

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

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

效果预览

这是选择前:

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

选择:

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

选择后:

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

代码

很基础的写法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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选择路径功能的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zjiang1994/article/details/53513377

延伸 · 阅读

精彩推荐