Ask Your Question
1

How to use Tkinter's filedialog to choose folders and files?

asked 2023-06-22 23:30:08 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-22 23:34:01 +0000

huitzilopochtli gravatar image

To use Tkinter's filedialog to choose folders and files, you can follow these steps:

  1. Import the necessary modules:
from tkinter import *
from tkinter import filedialog
  1. Create a tkinter window or root:
root = Tk()
  1. Define a function to open the file dialog box and get the path of the selected folder or file:
def open_file_dialog():
    path = filedialog.askopenfilename()  # to get files
    # path = filedialog.askdirectory()  # to get folders
    print(path)
  1. Create a button in the tkinter window or root and assign the above function as its command:
btn = Button(root, text="Open File Dialog", command=open_file_dialog)
btn.pack()
  1. Start the tkinter's mainloop:
root.mainloop()

This will create a button called "Open File Dialog" and when you click on it, the file dialog box will open and allow you to choose the desired folder or file. The path of the selected folder or file will then be printed on the console.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-22 23:30:08 +0000

Seen: 13 times

Last updated: Jun 22 '23