- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make a new folder using askdirectory dialog in Tkinter?
To make a new folder using askdirectory dialog in Tkinter, we can take the following steps −
Import the required modules. filedialog module is required for askdirectory method. os module is required for makedirs method.
Create an instance of tkinter frame.
Set the size of the frame using win.geometry method.
Define a user-defined method "create_subfolder". Inside the method, call filedialog.askdirectory to select a folder and save the path in a variable, source_path.
We can use askdirectory method of filedialog to open a directory. Save the path of the selected directory in a 'path' variable.
Then, use os.path.join and makedirs to create a sub-folder inside the parent directory.
Create a button to call the create_subfolder method.
# Import the required libraries from tkinter import * from tkinter import ttk from tkinter import filedialog import os # Create an instance of tkinter frame or window win = Tk() # Set the size of the window win.geometry("700x350") def create_subfolder(): source_path = filedialog.askdirectory(title='Select the Parent Directory') path = os.path.join(source_path, 'Images') os.makedirs(path) button1 = ttk.Button(win, text="Select a Folder", command=create_subfolder) button1.pack(pady=5) win.mainloop()
When we execute the above code, it will first show the following window −
Now, click the "Select a Folder" button to select a parent folder. It will automatically create a sub-folder called "Images" in the selected parent folder.
- Related Articles
- How to create a modal dialog in tkinter?
- Creating a prompt dialog box using Tkinter?
- How to get a Popup Dialog in Tkinter/Python?
- How to give Tkinter file dialog focus?
- How to make custom dialog in android?
- How to make custom dialog with custom dialog view actions in android?
- How to read multiple text files from a folder in Python?(Tkinter)
- How to make a Button using the Tkinter Canvas widget?
- How to make full screen custom dialog in Android?
- Save File Dialog Box in Tkinter
- How to make custom dialog with rounded corners in android?
- How to make a Tkinter widget invisible?
- How to make a system tray application in Tkinter?
- How to zip a folder recursively using Python?
- How to share a windows folder using PowerShell?