Why the tkinter function automatically repeats twice?

Viewed 33

When I try to build a simple UI, everything works well, except the tkinter function I used always repeats twice, I am not sure why this happens and how to deal with it. Here is an example code:

import tkinter as tk

root = tk.Tk()
dirname = tk.filedialog.askdirectory(parent=root, initialdir="/",title='Please select a directory')
print(dirname)

When I run this code, the function works well, it will let me select a folder, but after I select the folder, it asks me to select it again. And from the print(), it outputs both the folders I have selected.

What should I do to make it only run one time?

1 Answers

Hi can you try with below script, I tried running below script and it does not ask for second selection and dont print second selection as well;

import tkinter as tk
from tkinter import filedialog as fd

root = tk.Tk()
dirname = fd.askdirectory(parent=root, initialdir="/",title='Please select a  
    directory')
print(dirname)
Related