difference between askopenfile() and askopenfilename()
The function askopenfilename() returns the selected file name. ( path as string ).
The function askopenfile() returns the opened file object in read mode.
In above code both can be used and watch the commented part of the code below for comparison of both functions. In both cases output is same.
#file = filedialog.askopenfile() # returns file object
file = filedialog.askopenfilename() # returns path as string
file=open(file,'r') # file object is created
We can use askdirectory() to select any directory in your system as string. Here we are opening one dialog box to browse and select a folder. On selection of the folder, path will be displayed on a Label. User can change the selection also.
Tkinter filedialog askdirectory() to show dialog window to select directory & return path as string
import tkinter as tk
from tkinter import filedialog
my_w = tk.Tk()
my_w.geometry("400x200") # Size of the window
my_w.title("www.plus2net.com") # title
my_dir='' # string to hold directory path
my_dir = filedialog.askdirectory() # select directory
l1.config(text=my_dir) # update the text of Label with directory path
my_w.mainloop() # Keep the window open
Project : GUI Text Editor
Text Editor with all file handling operations like New, Open., Save, Save As and Close to add or update data of the file. The tkinter filedialog is used to display file handling dialog boxes and Menu is used to execute various functions to manage a file. Tkinter Text Editor to Manage File Operations »