Open File Dialog in Tkinter (GUI Programming) – Python Tkinter Tutorial

Open File Dialog in Tkinter Python provides a vast number of libraries for GUI application development. Tkinter is the widely used library for GUI application development. Using the Tkinter library of python which carries large numbers of widgets, we can easily create a Graphical User Interface for our application.

Open File Dialog: Like other programming languages, Tkinter library of Python also provides functionality to perform the operation of open file, save file or directories. In this guide we are going to talk about open file dialog which is used when we have to open file or directories.

tkinter tutorial

In other programming languages like Java, php etc. we have to write full code for implementing open file dialog operation. But in Tkinter, this functionality is already defined.

Let’s see one example to understand this concept better

Example 1:

from tkinter import *
from tkinter import filedialog
#setting up parent window
root = Tk()

#function definition for opening file dialog
def openf():
    file = filedialog.askopenfilename(initialdir='/', title="select file")

file_open = Button(root, text="Open file", command= openf)
file_open.pack(pady=20)

root.geometry("350x200")
root.title("PythonLobby.com")
root.mainloop()

Output:
open file dialog in tkinter

example of file dialog in tkinter

Program explanation in detail:

  • First, we have imported all the required libraries and modules of Tkinter using import *.
  • Then we have initialized root as an object for Tk() class for creating a root window.
  • Next we defined the opendf() function which we will call on button click later.
  • In function definition we used filedialog library along with askopenfilename function.
  • Here in function two parameter is passed i.e. initialdir=’\’ it means root directory where our current project file available and title will display at the top of application.
  • Next we created one button. On button click the above function will be called and the function will perform the required operation.
  • Rest below given code is for configuring our application layout i.e. width, height and title.

Example 2: In this example, we will use text editor with open file dialog. We will open a text file and try to insert its content into our text editor.

from tkinter import *
from tkinter import filedialog
#setting up parent window
root = Tk()

#function definition for opening file dialog
def openf():
    file = filedialog.askopenfilename(initialdir='/', title="select file",
                                      filetypes=(("Txt Files",".txt"), ("All Files","*.*")))
    #inserting data to text editor
    content =open(file)
    data = content.read()
    teditor.insert(END, data)

#creating text editor
teditor = Text(root, width=40, height=8)
teditor.pack(pady=10)
file_open = Button(root, text="Open file", command= openf)
file_open.pack()

root.geometry("350x200")
root.title("PythonLobby.com")
root.mainloop()

Output:
open file dialog in tkinter

example of file dialog in tkinter

Program explanation in detail:

  • First, we have imported all the required libraries and modules of Tkinter using import *.
  • Then we have initialized root as an object for Tk() class for creating a root window.
  • Next we defined the opendf() function which we will call on button click later.
  • In function definition we used filedialog library along with askopenfilename function.
  • Here in function two parameter is passed i.e. initialdir=’\’ it means root directory where our current project file available and title will display at the top of application.
  • Next we created one button. On button click the above function will be called and the function will perform the required operation.
  • Additionally in this program we implemented the concept of file handling as well for opening file and reading the file.
  • Rest below given code is for configuring our application layout i.e. width, height and title.