In this Python TKinter article i want to show How To Browse A File In Python TKinter ,
if you are interested in Python GUI Development, you can check the below links. the
tkinter.filedialog
module provides classes and factory functions for creating file/
directory selection windows.
Also you can read more article on Python GUI Development
1: PyQt5 GUI Development Tutorials
2: Pyside2 GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: Kivy GUI Development Tutorials
Python Web Development Tutorials
1: Flask Web Development Tutorials
2: Django Web Development Tutorials
OK now 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("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() def button(self): self.button = ttk.Button(self.labelFrame, text = "Browse A File",command = self.fileDialog) self.button.grid(column = 1, row = 1) 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) root = Root() root.mainloop() |
So first of all we need to import the required classes fro 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
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email