In this Python TKinter article i want to show How To Browse A File In Python TKinter , for more TKinter articles check the below links.
1: Introduction To TKinter GUI Development
2: How To Create Label In Python TKinter
3: How To Create GUI Button In TKinter
4: How To Create TextBox In TKinter
5: How To Create ComboBox In TKinter
6: How To Create CheckButton In Python TKinter
7: How To Create RadioButton In Python TKinter
8: How To Create ScrollTextArea In TKinter
9: Python TKinter Creating LabelFrame
10: How To Create MenuItem In TKinter
11: Python TKinter Creating MenuItem Events
12: How To Create TabWidgets In TKinter
13: How Add Widgets In TKinter Tab Controls
14: How To Create MessageBox In TKinter
15: How To Create MultiChoiceBox In Python TKinter
16: How To Create ProgressBar In Python TKinter
17: How To Create ProgressBar In Python TKinter
18: How To Create Canvas In Python TKinter
19: How To Embed Matplotlib In TKinter
20: How To Add Legends In Matplotlib TKinter
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() |
This is our Root class that extends from TK, and we add our window requirements like title, size and icon in this class, also we add 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