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

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

以上这篇Python3 Tkinter选择路径功能的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

matplotlib简介,安装和简单实例代码

matplotlib简介,安装和简单实例代码

官网介绍: Matplotlib is a Python 2D plotting library which produces publication quality figures i...

python筛选出两个文件中重复行的方法

本文实例为大家分享了python脚本筛选出两个文件中重复的行数,供大家参考,具体内容如下 ''' 查找A文件中,与B文件中内容不重复的内容 ''' #!usr/bin/python...

Python Tkinter GUI编程入门介绍

Python Tkinter GUI编程入门介绍

一、Tkinter介绍 Tkinter是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口。Tkinter不是唯一的python图形编程接口,但是是其中...

pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法

pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法

self.lineEdit.setEchoMode(QLineEdit.Password) 设置密码隐藏 self.lineEdit.setClearButtonEnabled(True...

python 文件与目录操作

1)os.path 1.1 os.path.isabs(path) 是否是绝对路径 1.2 os.path.isfile(path) 1.3 os.path.isdir(path) 1....