大家好,又见面了,我是你们的朋友全栈君。
import tkinter as tk
from tkinter import filedialog
import os
application_window = tk.Tk()
# 设置文件对话框会显示的文件类型
my_filetypes = [('all files', '.*'), ('text files', '.txt')]
# 请求选择文件夹/目录
answer = filedialog.askdirectory(parent=application_window,
initialdir=os.getcwd(),
title="Please select a folder:")
# 请求选择文件
answer = filedialog.askopenfilename(parent=application_window,
initialdir=os.getcwd(),
title="Please select a file:",
filetypes=my_filetypes)
# 请求选择一个或多个文件
answer = filedialog.askopenfilenames(parent=application_window,
initialdir=os.getcwd(),
title="Please select one or more files:",
filetypes=my_filetypes)
# 请求选择一个用以保存的文件
answer = filedialog.asksaveasfilename(parent=application_window,
initialdir=os.getcwd(),
title="Please select a file name for saving:",
filetypes=my_filetypes)
有一点需要注意,开头的 from tkinter import filedialog
不能写为 from tkinter import *
代码中的answer
直接就是绝对路径了。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/179083.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有