In this Python TKinter article i want to show you How to Browse a File in Python TKinter, so first of all let’s talk about FileDialog in Python TKinter.
What is FileDialog in TKinter?
In Tkinter, FileDialog is a module that provides a dialog window for the user to select files or directories from the file system. This module is typically used when you want to prompt the user to select a file for opening or saving inside your Tkinter application.
FileDialog module provides classes such as Open and SaveAs to create dialog boxes for opening and saving files. These classes allows you to specify different options for the dialog window, such as the file types to filter, initial directory and window title.
This is the complete code for How To Browse A File In Python TKinter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from tkinter import * from tkinter import ttk from tkinter import filedialog class Root(Tk): def __init__(self): super(Root, self).__init__() self.title("Codeloop - Tkinter Browsing") # Set window title # Set window icon self.iconphoto(True, PhotoImage(file='codeloop.png')) # Create labeled frame to hold the file browsing elements self.labelFrame = ttk.LabelFrame(self, text="Open File") self.labelFrame.grid(column=0, row=1, padx=20, pady=20) # Create button to trigger the file dialog self.button() def button(self): # Create button inside the labeled frame self.button = ttk.Button(self.labelFrame, text="Browse A File", command=self.fileDialog) self.button.grid(column=1, row=1) def fileDialog(self): # Open file dialog to select a file self.filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetype=(("jpeg files", "*.jpg"), ("all files", "*.*"))) # Create label to display the selected filename self.label = ttk.Label(self.labelFrame, text="") self.label.grid(column=1, row=2) # Display the selected filename self.label.configure(text=self.filename) # Create an instance of the Root class root = Root() root.mainloop() |
So first of all we need to import the required classes from TKinter library.
1 2 3 |
from tkinter import * from tkinter import ttk from tkinter import filedialog |
This is our Root class that extends from TK,and we have add our window requirements like title, size and icon in this class, also we have added a labelframe in this class.
1 2 3 4 5 6 7 8 9 10 11 |
class Root(Tk): def __init__(self): super(Root, self).__init__() self.title("Python Tkinter Dialog Widget") self.minsize(640, 400) self.wm_iconbitmap('icon.ico') self.labelFrame = ttk.LabelFrame(self, text = "Open File") self.labelFrame.grid(column = 0, row = 1, padx = 20, pady = 20) self.button() |
In this method we create a button and we will add an event to this button, because when a user clicks on this button i want to open browsing dialog.
1 2 3 |
def button(self): self.button = ttk.Button(self.labelFrame, text = "Browse A File",command = self.fileDialog) self.button.grid(column = 1, row = 1) |
This is the method that we are going to browse our files, and this method is connected to our button at the top.
1 2 3 4 5 6 7 |
def fileDialog(self): self.filename = filedialog.askopenfilename(initialdir = "/", title = "Select A File", filetype = (("jpeg files","*.jpg"),("all files","*.*")) ) self.label = ttk.Label(self.labelFrame, text = "") self.label.grid(column = 1, row = 2) self.label.configure(text = self.filename) |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email