In this tutorial, we will learn how to upload files in Python Tkinter. We are working on Linux operating system but this code can be executed on Windows and Mac. So let us find out how to upload a file in Python Tkinter.
Upload Files in Python Tkinter
- This project demonstrates the functionality of selecting and uploading files also demonstrates the working of the progress bar in Python. To learn more about the progress bar please click on this Url.
- tkinter.filedialog module is used to access askopenfile function. This allows users to select files using file explorer. To learn more about tkinter filedialog please click on this Url
from tkinter import * from tkinter.ttk import * from tkinter.filedialog import askopenfile import time ws = Tk() ws.title('PythonGuides') ws.geometry('400x200') def open_file(): file_path = askopenfile(mode='r', filetypes=[('Image Files', '*jpeg')]) if file_path is not None: pass def uploadFiles(): pb1 = Progressbar( ws, orient=HORIZONTAL, length=300, mode='determinate' ) pb1.grid(row=4, columnspan=3, pady=20) for i in range(5): ws.update_idletasks() pb1['value'] += 20 time.sleep(1) pb1.destroy() Label(ws, text='File Uploaded Successfully!', foreground='green').grid(row=4, columnspan=3, pady=10) adhar = Label( ws, text='Upload Government id in jpg format ' ) adhar.grid(row=0, column=0, padx=10) adharbtn = Button( ws, text ='Choose File', command = lambda:open_file() ) adharbtn.grid(row=0, column=1) dl = Label( ws, text='Upload Driving License in jpg format ' ) dl.grid(row=1, column=0, padx=10) dlbtn = Button( ws, text ='Choose File ', command = lambda:open_file() ) dlbtn.grid(row=1, column=1) ms = Label( ws, text='Upload Marksheet in jpg format ' ) ms.grid(row=2, column=0, padx=10) msbtn = Button( ws, text ='Choose File', command = lambda:open_file() ) msbtn.grid(row=2, column=1) upld = Button( ws, text='Upload Files', command=uploadFiles ) upld.grid(row=3, columnspan=3, pady=10) ws.mainloop()
This is the output of the above code. In this user need to submit three documents in jpeg format. The interface has 3 buttons to select the documents and one button to upload the documents.
Once the user can selected the file then he/she need to click on the upload file button to send the file on the server. We have not connected this application with any server. User will see a progress bar that shows the uploading in progress.
Once all the files will be uploaded, the application will display success message as you can see int the below picture.
You may like the following Python tutorials:
- File does not exist Python
- How to read video frames in Python
- Python program to print element in an array
- Python concatenate list with examples
- Python Tkinter drag and drop
- Python program to print prime numbers
- Python Tkinter Quiz
In this Python tutorial, we learned how to upload a file in Python Tkinter.