Python 文件选择框askopenfilename的参数

本文介绍如何使用Python的Tkinter模块中的askopenfilename函数打开文件选择对话框,详细解释了函数参数如title, filetypes和initialdir的设置方法,并展示了如何获取所选文件的绝对路径。

需求:打开一个文件选择框(单选.exe)

askopenfilename的参数,返回值为选择文件的绝对路径
from tkinter.filedialog import askopenfilename
# For the following classes and modules:
# options (all have default values):
# - defaultextension: added to filename if not explicitly given
# - filetypes: sequence of (label, pattern) tuples.  the same pattern may occur with several patterns.  use "*" as pattern to indicate all files.
# - initialdir: initial directory.  preserved by dialog instance.
# - initialfile: initial file (ignored by the open dialog).  preserved by dialog instance.
# - parent: which window to place the dialog on top of
# - title: dialog title
# - multiple: if true user may select more than one file
# options for the directory chooser:
# - initialdir, parent, title: see above
# - mustexist: if true, user must pick an existing directory
from tkinter.filedialog import askopenfilename
file_path=askopenfilename(title='Select the diagnostic instrument .exe file', filetypes=[('EXE', '*.exe'), ('All Files', '*')],initialdir='C:\\Windows')

在这里插入图片描述

但是会多出一个主界面
在这里插入图片描述

如果你原先有个界面,这个空白界面就不会出现了。

函数返回值为绝对路径

在这里插入图片描述

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值