0

i'm working with the Tk module, to select files and get their path. After selecting file the Tk window doesn't close. It remains open even the code is over.

Before saying creating this post, i tried to put different solution(No duplicate):root.destroy();root.withdraw();root.quit()

But it doesn't works.

Here is the code:

from tkinter import filedialog
from tkinter import Tk


def get_path():

    root = Tk()
    root.filename =  filedialog.askopenfilenames()
    return (root.filename)
    root.quit()

e=get_path()
print(e)

enter image description here

Thanks in advance for your help.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Tom92
  • 5
  • 1
  • 3

1 Answers1

0

"askopenfilename" is used to get "files" only, such as .py, .txt, .odt files etc. So, the window will close if you selected a file.

Whereas, if you want to get the path to a "directory", use "askdirectory" :

import tkinter as tk    
from tkinter import filedialog

root = tk.Tk()
root.filename =  filedialog.askdirectory()
return (root.filename)
root.quit()
Eshita Shukla
  • 791
  • 1
  • 8
  • 30