以下代码来自http://interactivepython.org/runestone/static/thinkcspy/GUIandEventDrivenProgramming/02_standard_dialog_boxes.html#file-chooser
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26import 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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复