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()

 

運行效果: 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM