文章目录
- 1 文件
- 1.1 获取单个文件名称:askopenfilename()
- 1.2 获取多个文件名称:askopenfilenames()
- 1.3 获取单个文件属性:askopenfile()
- 1.4 获取多个文件属性:askopenfiles()
- 1.5 获取保存文件的路径:asksaveasfilename()
- 1.6 获取保存文件的属性:asksaveasfile()
- 2 目录
- 2.1 获取目录名称:askdirectory()
1 文件
修改中,请稍等...
1.1 获取单个文件名称:askopenfilename()
import tkinter
from tkinter import filedialog
class FileDiaLogDemo(object):
def __init__(self):
window = tkinter.Tk()
window.withdraw() # 不显示窗体
def get_file_name(self):
# 选择单个文件(绝对路径)
file_path = filedialog.askopenfilename()
print(file_path)
if __name__ == '__main__':
test = FileDiaLogDemo()
test.get_file_name()
测试结果(参考):
C:/Users/Administrator/Desktop/Temp/1.txt
1.2 获取多个文件名称:askopenfilenames()
import tkinter
from tkinter import filedialog
class FileDiaLogDemo(object):
def __init__(self):
window = tkinter.Tk()
window.withdraw() # 不显示窗体
def get_file_names(self):
# 选择多个文件(绝对路径,Ctrl + 文件)
file_path = filedialog.askopenfilenames()
print(f'返回值类型:{type(file_path)}') # <class 'tuple'>
print(file_path)
if __name__ == '__main__':
test = FileDiaLogDemo()
test.get_file_names()
测试结果(参考):
('C:/Users/Administrator/Desktop/Temp/1.txt',
'C:/Users/Administrator/Desktop/Temp/2.txt')
1.3 获取单个文件属性:askopenfile()
import tkinter
from tkinter import filedialog
class FileDiaLogDemo(object):
def __init__(self):
window = tkinter.Tk()
window.withdraw() # 不显示窗体
def get_file(self):
# 选择单个文件
file = filedialog.askopenfile()
print(f'文件的名称:{file.name}')
print(f'文件的模式:{file.mode}')
print(f'文件的编码:{file.encoding}')
if __name__ == '__main__':
test = FileDiaLogDemo()
test.get_file()
1.4 获取多个文件属性:askopenfiles()
import tkinter
from tkinter import filedialog
class FileDiaLogDemo(object):
def __init__(self):
window = tkinter.Tk()
window.withdraw() # 不显示窗体
def get_files(self):
# 选择单个文件
file = filedialog.askopenfiles()
print(f'返回值类型:{type(file)}') # <class 'list'>
print(f'文件的个数:{len(file)}')
if __name__ == '__main__':
test = FileDiaLogDemo()
test.get_files()
1.5 获取保存文件的路径:asksaveasfilename()
import tkinter
from tkinter import filedialog
class FileDiaLogDemo(object):
def __init__(self):
window = tkinter.Tk()
window.withdraw() # 不显示窗体
def get_save_file_name(self):
file = filedialog.asksaveasfilename(title="请选择文件存储路径",
initialdir=r'E:\02 源码',
filetypes=[('文本文档', '.txt'),
('Excel', '.xls .xlsx'),
('All Files', ' *')],
defaultextension='.png')
print(file)
if __name__ == '__main__':
test = FileDiaLogDemo()
test.get_save_file_name()
测试结果(参考):
C:/Users/Administrator/Desktop/Temp/1.txt
1.6 获取保存文件的属性:asksaveasfile()
import tkinter
from tkinter import filedialog
class FileDiaLogDemo(object):
def __init__(self):
window = tkinter.Tk()
window.withdraw() # 不显示窗体
def get_save_file(self):
file = filedialog.asksaveasfile(title="请选择文件存储路径",
initialdir=r'E:\02 源码',
filetypes=[('文本文档', '.txt'),
('Excel', '.xls .xlsx'),
('All Files', ' *')],
defaultextension='.png')
print(f'文件的名称:{file.name}')
print(f'文件的模式:{file.mode}')
print(f'文件的编码:{file.encoding}')
if __name__ == '__main__':
test = FileDiaLogDemo()
test.get_save_file()
测试结果(参考):
文件的名称:C:/Users/Administrator/Desktop/Temp/3.txt
文件的模式:w
文件的编码:cp936
2 目录
2.1 获取目录名称:askdirectory()
import tkinter
from tkinter import filedialog
class FileDiaLogDemo(object):
def __init__(self):
window = tkinter.Tk()
window.withdraw() # 不显示窗体
def get_directory_name(self):
# 获取单个目录名称(绝对路径)
directory = filedialog.askdirectory()
print(directory)
if __name__ == '__main__':
test = FileDiaLogDemo()
test.get_directory_name()
测试结果(参考):
C:/Users/Administrator/Desktop/Temp/01