Python之Tkinter包的filedialog模块介绍

Python之Tkinter包的filedialog模块介绍

在程序运行该过程中,当你需要手动选择文件或手动选择文件存储路径时,就需要用到tkinter库中filedialog提供的函数。tkinter的filedialog模块提供了一个简单的对话框界面,用于让用户选择文件或目录。它通常与tkinter一起使用,用于创建图形用户界面(GUI)应用程序。Tkinter库是python默认的GUI库,它是python的内置库不需要安装。

官方介绍见https://docs.python.org/zh-cn/3/library/dialog.html#module-tkinter.filedialog

filedialog模块包含了一些常用的对话框,包括:

1. askopenfilename函数:打开文件对话框,用于选择一个文件,返回文件路径,类型为字符串。语法格式如下:askopenfilename(**options),其中**options是可选的关键字参数。

2. askopenfilenames函数:打开文件对话框,用于选择多个文件,返回一个元组,包括所有选择文件的路径。语法格式如下:askopenfilenames(**options),其中**options是可选的关键字参数。

3. asksaveasfilename函数:保存文件对话框,用于选择文件的保存路径和文件名。语法格式如下:asksaveasfilename(**options) ,其中**options是可选的关键字参数。

4. askdirectory函数:选择目录对话框,用于选择一个目录,返回目录路径。语法格式如下:askdirectory(**options) ,其中**options是可选的关键字参数。

这些对话框提供了用户友好的界面,使得用户可以方便地浏览文件系统,选择文件或目录。在tkinter应用程序中,可以使用filedialog模块来创建这些对话框,以便用户可以与文件系统进行交互。

**options关键字参数常用的有:

☆ title  指定文件对话框的标题栏文本。

☆ defaultextension  指定文件的后缀,例如:defaultextension='.jpg',那么当用户输入一个文件名'Python'的时候,文件名会自动添加后缀为'Python.jpg' 。注意:如果用户输入文件名包含后缀,那么该选项不生效。允许使用 “*” 通配符。

☆ filetypes  指定筛选文件类型的下拉菜单选项,该选项的值是由二元组构成的列表,每个二元组是由(类型名,后缀)构成,例如:filetypes=[('文本', '.txt'), ('栅格', '.tif'), ('动图', '.gif')]。(看情况,当文件夹中文件很多且类型很多,建议给出该参数)

☆ initialdir  指定打开保存文件的默认路径,默认路径是当前文件夹。

☆ multiple  是否确定选择多个文件,if true user may select more than one file。

例如

选择一个文件:

filedialog.askopenfilename(title='请选择一个文件', initialdir=r'D:\数据\测试数据', filetypes=[("文本文档", ".txt"), ('Excel', '.xls .xlsx'), ('All Files', ' *')], defaultextension='.tif', multiple=True)

选择多个文件:

filedialog.askopenfilename(title='请选择多个文件', initialdir=r'D:\数据\测试数据', filetypes=[( "文本文档", ".txt"), ('Excel', '.xls .xlsx'), ('All Files', ' *')])

选择文件存储路径:

filedialog.asksaveasfile(title='请选择文件存储路径', initialdir=r'D:\数据\测试数据', filetypes=[( "文本文档", ".txt"), ('Excel', '.xls .xlsx'), ('All Files', ' *')], defaultextension='.tif')

选择文件夹:

filedialog.askdirectory(title='选择存放的位置!', initialdir=r'D:\数据\测试数据')

★简单的示例,使用模块tkinte的filedialog模块来创建打开文件对话框:

import tkinter as tk
from tkinter import filedialog

def open_file_dialog():
    file_path = filedialog.askopenfilename()
    if file_path:
        print("选择的文件路径为:", file_path)

# 创建tkinter窗口
root = tk.Tk()
root.geometry("300x200")  # 设置宽度为300像素,高度为200像素
root.title("文件对话框示例")

# 创建打开文件按钮
open_button = tk.Button(root, text="打开文件", command=open_file_dialog)
open_button.pack(pady=20)

运行效果:

★获得文件全名

窗体上有一个按钮,其右边有一个文本框,单击按钮选择好的文件全名放到文本框中:

import tkinter as tk
from tkinter import filedialog

def choose_file():
    file_path = filedialog.askopenfilename() #文件的全名(包括路径、文件名和文件扩展名)
    entry.delete(0, tk.END)  # 清空文本框
    entry.insert(0, file_path)  # 将选择的文件路径插入文本框

# 创建窗口
window = tk.Tk()
window.geometry("500x200")  # 设置宽度为500像素,高度为200像素
window.title("文件选择示例")

# 创建按钮
button = tk.Button(window, text="选择文件", command=choose_file)
button.pack(side=tk.LEFT)

# 创建文本框
entry = tk.Entry(window, width=50)
entry.pack(side=tk.LEFT)

在这个示例中,我们创建了一个窗口,放置了一个按钮和一个文本框。单击按钮会触发choose_file函数,该函数使用filedialog.askopenfilename()来选择文件,并将选择的文件路径放入文本框中。运行效果:

☆ 修改上面例子,获得文件名(不含路径):

import tkinter as tk
from tkinter import filedialog
import os

def choose_file():
    file_path = filedialog.askopenfilename()
    filename = os.path.basename(file_path)  # 获取文件名
    entry.delete(0, tk.END)  # 清空文本框
    entry.insert(0, filename)  # 将文件名插入文本框

# 创建窗口
window = tk.Tk()
window.geometry("500x200")  # 设置宽度为500像素,高度为200像素
window.title("文件选择示例:获取文件名")

# 创建按钮
button = tk.Button(window, text="选择文件", command=choose_file)
button.pack(side=tk.LEFT)

# 创建文本框
entry = tk.Entry(window, width=50)
entry.pack(side=tk.LEFT)

# 运行窗口
window.mainloop()

示例中,使用os.path.basename()函数获取了文件的文件名,并将其放入文本框中。这样就可以实现仅获取文件名(不包含路径)的功能。

os.path模块是Python标准库的一部分,已经随着Python的安装而包含在内。os.path模块提供了许多用于处理文件路径的方法,包括获取文件名、扩展名、目录名等功能。因此,在使用Python时,你可以直接引入os.path模块,而无需额外安装。https://docs.python.org/zh-cn/3/library/os.path.html#module-os.path 

假设文件全名file_path = "d:/your/file.txt"

os.path.basename()函数来获取文件名:file_name = os.path.basename(file_path)  # 获取文件名

os.path.splitext()函数来获取文件扩展名:file_extension = os.path.splitext(file_path)[1]  # 获取文件扩展名

先使用os.path.basename()函数获取文件名,然后再使用os.path.splitext()函数获取文件扩展名:file_name_without_extension = os.path.splitext(os.path.basename(file_path))[0]  # 获取不带扩展名的文件名

★获取选取文件的路径

窗体上有一个按钮,其右边有一个文本框,单击按钮选择好的文件的路径(不含文件名)放到文本框中:

import tkinter as tk
from tkinter import filedialog
import os

def choose_file():
    file_path = filedialog.askopenfilename()
    directory = os.path.dirname(file_path)  # 获取文件的目录路径
    entry.delete(0, tk.END)  # 清空文本框
    entry.insert(0, directory)  # 将文件的目录路径插入文本框

# 创建窗口
window = tk.Tk()
window.title("获取选取文件的路径")

# 创建按钮
button = tk.Button(window, text="选择文件", command=choose_file)
button.pack(side=tk.LEFT)

# 创建文本框
entry = tk.Entry(window, width=50)
window.geometry("500x200")  # 设置宽度为500像素,高度为200像素
entry.pack(side=tk.LEFT)

# 运行窗口
window.mainloop()

示例中,使用os.path.dirname()函数获取了文件的目录路径,并将其放入文本框中。这样就可以实现获取文件路径不包含文件名的功能。

附录

关于计算机windows操作系统中的目录、文件夹 、路径更多情况可见https://blog.csdn.net/cnds123/article/details/105169800

python文件或文件夹操作https://blog.csdn.net/cnds123/article/details/123583020

Python 的Tkinter包系列之四:对话框https://blog.csdn.net/cnds123/article/details/127392512

猜你喜欢

转载自blog.csdn.net/cnds123/article/details/134906506