Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | ToDo GUI Application using Tkinter
Next article icon

Prerequisites:Introduction to Tkinter 


Python offers various modules to create graphics programs. Out of these Tkinter provides the fastest and easiest way to create GUI applications.
 

The following steps are involved in creating a tkinter application: 

  • Importing the Tkinter module. 
  • Creation of the main window (container). 
  • Addition of widgets to the main window 
  • Applying the event Trigger on widgets like buttons, etc. 
     

The GUI would look like below:

Creating the File Explorer


In order to do so, we have to import thefiledialog module from Tkinter. The File dialog module will help you open, save files or directories.
In order to open a file explorer, we have to use the method, askopenfilename(). This function creates a file dialog object.
 

Syntax: tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("file_type","*.extension"),("all files","*.*")))
Parameters: 
 

  1. initialdir: We have to specify the path of the folder that is to be opened when the file explorer pops up.
  2. title: The title of file explorer opened.
  3. filetypes: Here we can specify different kinds of file extensions so that the user can filter based on different file types


Below is the implementation
 

Python3
# Python program to create# a file explorer in Tkinter# import all components# from the tkinter libraryfromtkinterimport*# import filedialog modulefromtkinterimportfiledialog# Function for opening the# file explorer windowdefbrowseFiles():filename=filedialog.askopenfilename(initialdir="/",title="Select a File",filetypes=(("Text files","*.txt*"),("all files","*.*")))# Change label contentslabel_file_explorer.configure(text="File Opened: "+filename)# Create the root windowwindow=Tk()# Set window titlewindow.title('File Explorer')# Set window sizewindow.geometry("500x500")#Set window background colorwindow.config(background="white")# Create a File Explorer labellabel_file_explorer=Label(window,text="File Explorer using Tkinter",width=100,height=4,fg="blue")button_explore=Button(window,text="Browse Files",command=browseFiles)button_exit=Button(window,text="Exit",command=exit)# Grid method is chosen for placing# the widgets at respective positions# in a table like structure by# specifying rows and columnslabel_file_explorer.grid(column=1,row=1)button_explore.grid(column=1,row=2)button_exit.grid(column=1,row=3)# Let the window wait for any eventswindow.mainloop()

Output:


 

takinter-filedialog1


 

takinter-filedialog1


 

takinter-filedialog1


 


File Explorer in Python using Tkinter

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp