2

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?

  • Are you importing this file? Is running this code in the REPL (e.g. IDLE) enough to properly replicate the issue? – wizzwizz4 Jul 29 '22 at 10:54
  • Cannot reproduce the issue. Note that `tk.filedialog.askdirectory(...)` cannot be executed. You need to import the `filedialog` sub-module. – acw1668 Jul 29 '22 at 11:53
  • As written this code will not run since you didn't import tk.filedialog. Please provide an example that actually reproduces the problem. – Bryan Oakley Jul 29 '22 at 15:59

1 Answers1

0

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)
Lanre
  • 340
  • 2
  • 6
  • Well, it is the same, but I find if I create new project and only make the UI, the problem will not happen, so there may be a problem with the environment or other parts of the code, thanks. – tangjikededela Jul 29 '22 at 14:42