Tkinter文件选择框 tkinter.filedialog
打开文件
from tkinter import filedialog
img_path = filedialog.askopenfilename()
print(img_path)
from tkinter.filedialog import askopenfilename, asksaveasfilename
tkinter.filedialog
'''
打开图像文件路径
'''
def open_imgfile():
img_path = askopenfilename(
title='请选择图片文件',
filetypes=[('图片', '.jpg .png .gif .bmp .jpeg')],
)
if img_path and img_path.endswith(('.jpg', '.png', '.gif', '.jpeg', '.bmp')):
path = img_path
打开文件夹
from tkinter.filedialog import askdirectory
dir = askdirectory(title='请选择文件夹',)
print(dir)
实例
uitls.py
from tkinter.filedialog import askopenfilename,askdirectory
def openfile():
file_path = askopenfilename(title='请选择文件')
return file_path
def opendir():
dir_path = askdirectory(title='请选择文件夹')
return dir_path