我是靠谱客的博主 迅速身影,这篇文章主要介绍Python 打开文件对话框,现在分享给大家,希望可以做个参考。

以下代码来自http://interactivepython.org/runestone/static/thinkcspy/GUIandEventDrivenProgramming/02_standard_dialog_boxes.html#file-chooser

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直接就是绝对路径了。

最后

以上就是迅速身影最近收集整理的关于Python 打开文件对话框的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(107)

评论列表共有 0 条评论

立即
投稿
返回
顶部