Below is the complete implementation

Python




# Python program to specify the file
# path in a tkinter file dialog
  
# Import the libraries tk, ttk, filedialog
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
  
# Create a GUI app
app = tk.Tk()
  
# Specify the title and dimensions to app
app.title('Tkinter Dialog')
app.geometry('600x350')
  
# Create a textfield for putting the
# text extracted from file
text = tk.Text(app, height=12)
  
# Specify the location of textfield
text.grid(column=0, row=0, sticky='nsew')
  
# Create a function to open the file dialog
  
  
def open_text_file():
  
    # Specify the file types
    filetypes = (('text files', '*.txt'),
                 ('All files', '*.*'))
  
    # Show the open file dialog by specifying path
    f = fd.askopenfile(filetypes=filetypes,
                       initialdir="D:/Downloads")
  
    # Insert the text extracted from file in a textfield
    text.insert('1.0', f.readlines())
  
  
# Create an open file button
open_button = ttk.Button(app, text='Open a File',
                         command=open_text_file)
  
# Specify the button position on the app
open_button.grid(sticky='w', padx=250, pady=50)
  
# Make infinite loop for displaying app on the screen
app.mainloop()


Output:

 



How to specify the file path in a tkinter filedialog?

In this article, we are going to know how to specify the file path in a tkinter filedialog.

Are you creating a GUI app in which you want the user to directly open a specific location while selecting a file or saving a file? Then, you must definitely read this article. This article will discuss how to specify the file path in a Tkinter dialog.

Similar Reads

filedialog Module

This module includes a set of unique dialogs which can be used while dealing with files. It is specifically used for the file selection when you want to provide an option to user to browse a file or a directory from the system....

Stepwise Implementation:

Step 1: First of all, import the libraries, tk, ttk, and filedialog from Tkinter....

Below is the complete implementation:

Python # Python program to specify the file # path in a tkinter file dialog    # Import the libraries tk, ttk, filedialog import tkinter as tk from tkinter import ttk from tkinter import filedialog as fd    # Create a GUI app app = tk.Tk()    # Specify the title and dimensions to app app.title('Tkinter Dialog') app.geometry('600x350')    # Create a textfield for putting the # text extracted from file text = tk.Text(app, height=12)    # Specify the location of textfield text.grid(column=0, row=0, sticky='nsew')    # Create a function to open the file dialog       def open_text_file():        # Specify the file types     filetypes = (('text files', '*.txt'),                  ('All files', '*.*'))        # Show the open file dialog by specifying path     f = fd.askopenfile(filetypes=filetypes,                        initialdir="D:/Downloads")        # Insert the text extracted from file in a textfield     text.insert('1.0', f.readlines())       # Create an open file button open_button = ttk.Button(app, text='Open a File',                          command=open_text_file)    # Specify the button position on the app open_button.grid(sticky='w', padx=250, pady=50)    # Make infinite loop for displaying app on the screen app.mainloop()...