Python_tkinter(4)_上传文件
1.上传单个文件
import tkinter as tk from tkinter import filedialog def upload_file(): selectFile = tk.filedialog.askopenfilename() # askopenfilename 1次上传1个;askopenfilenames1次上传多个 entry1.insert(0, selectFile) root = tk.Tk() frm = tk.Frame(root) frm.grid(padx='20', pady='30') btn = tk.Button(frm, text='上传文件', command=upload_file) btn.grid(row=0, column=0, ipadx='3', ipady='3', padx='10', pady='20') entry1 = tk.Entry(frm, width='40') entry1.grid(row=0, column=1) root.mainloop()
运行结果如下
选择文件后
2.上传多个文件
import tkinter as tk from tkinter import filedialog def upload_files(): selectFiles = tk.filedialog.askopenfilenames( title='可选择1个或多个文件') # askopenfilename 1次上传1个;askopenfilenames1次上传多个 for selectFile in selectFiles: text1.insert(tk.END, selectFile + '\n') # 更新text中内容 text1.update() root = tk.Tk() frm = tk.Frame(root) frm.grid(padx='20', pady='30') btn = tk.Button(frm, text='上传文件', command=upload_files) btn.grid(row=0, column=0, ipadx='3', ipady='3', padx='10', pady='20') text1 = tk.Text(frm, width='55', height='15') text1.grid(row=0, column=1) root.mainloop()
运行效果: