赞
踩
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
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')
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()
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()
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
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
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。