2

I can't get a Tkinter file dialog box to close. This Tkinter file dialog box closes just fine if there is no input statement downstream. But with it, it does not close, and crashes the app.

Through a process of elimination, it seems input() downstream from the root.destroy() command is interfering with the window closing.

Anyone have a fix?

Here's the code:

import tkinter
from tkinter import filedialog

###### THIS CODE WORKS #########
# Make a top-level instance and hide since it is ugly and big.
root = tkinter.Tk()
root.withdraw()

# Make it almost invisible - no decorations, 0 size, top left corner.
root.overrideredirect(True)
root.geometry('0x0+0+0')

# Show window again and lift it to top so it can get focus,
# otherwise dialogs will end up behind the terminal.
root.deiconify()
root.lift()
root.focus_force()

# get the file path of the trades to load.
new_file_path = filedialog.askopenfilename()

# Get rid of the top-level instance once to make it actually invisible.
root.destroy()

###### WHEN INPUT IS ADDED IT DOESNT WORK######
input('Testing testing testing  ')

I've tried adding a time.sleep(1) just after root.destroy.

I've tried the solution here: Tkinter askopenfilename() won't close

I'm using OSX 10.11.4 with Pycharm.

Community
  • 1
  • 1
Emily
  • 2,129
  • 3
  • 18
  • 43
  • Your description of behavior and the (incomplete) code do not match. The `askopenfilename` call does not return until you close the modal dialog. Code after the call is not executed until that happens. I tried with py 3.5.2 and tk 8.6 on Win10 both from IDLE and a Command Prompt console, and the behavior matches what I expect. The box closes with either button. What OS, python version, and tcl/tk version? (The latter is displayed by IDLE in Help => About IDLE.) – Terry Jan Reedy Sep 04 '16 at 21:43
  • I just noticed that the linked SO question was from an OSX user and that you have an OSX tag. It appears that file dialog boxes work quite differently there than on Windows. So I can neither reproduce nor help. Sorry. – Terry Jan Reedy Sep 04 '16 at 21:51
  • It appears you are trying to ask for a file name without having a full Tkinter gui, have you tried the solution proposed in [this question](http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog) – scotty3785 Sep 06 '16 at 13:52
  • I just tried that solution. I'm getting the same problem if an input is downstream. The code I just tested: from tkinter.filedialog import askopenfilename root = tkinter.Tk() root.withdraw() filename = askopenfilename() input('Testing testing testing ') print(filename) – Emily Sep 09 '16 at 08:07

0 Answers0