1

I have a selection of excel data that I am analyzing, and have just recently added the ability for the user to open the file explorer and locate the file visually, as opposed to entering the file location on the command line. I found this question (and answer) to make the window appear, which worked for a while.

I am still using the command line for everything except locating the file. Currently, this is a skeleton of what I have to open the window (nearly identical to the answer of the question linked above)

Tk().withdraw()
data_file_path = askopenfilename()

# other code with prompts, mostly print statements

Tk().withdraw()
drug_library_path = askopenfilename()

Once the code reaches the first two lines of code, the command line just sits with a blinking cursor, like it's waiting for input (my guess, for askopenfilename() to return a file location), but nothing happens. I can't ctrl+C to get out of the program, either.

I have found this question, which is close to what I'm looking for, but I'm on Windows, not Mac, and I can't even get the window to open -- most questions I see talk about not being able to close the window.

Thanks for any help!

Note: At this point in the program, no data from excel has been loaded. This is one of the first lines that is ran.

  • I can't duplicate your problem, so I'm not really sure what's going wrong - but I will point out that you should not be calling `Tk()` more than once in any program. – jasonharper Jun 27 '18 at 22:09
  • Okay, I'll fix that and see if anything changes. If I have two different functions doing this, should i call `Tk()` once in each one? –  Jun 27 '18 at 22:17

3 Answers3

0

Try easygui instead. It's also built on tkinter, but unlike filedialog it's made to run without a full GUI.

Since you are using Windows, use this command in the command line (not in python) to install easygui:

py -m pip install easygui

then try this code:

import easygui

data_file_path = easygui.fileopenbox()

# other code with prompts, mostly print statements

drug_library_path = easygui.fileopenbox()
Novel
  • 13,406
  • 2
  • 25
  • 41
0

I had the same problem, but I found that the issue was that I was getting input with input() before I called askopenfilename() or fileopenbox().

from   tkinter            import Tk
from   tkinter.filedialog import askopenfilename

var = input()
Tk().withdraw()
filepath = askopenfilename()

I simply switched the positions of askopenfilename() (or fileopenbox()) and input(), and it worked as usual.

Tk().withdraw()
filepath = askopenfilename()
var = input()
0

If you want to use an internal module, you can import tkFileDialog, and call:

filename = tkFileDialog.askopenfilename(title="Open Filename",filetypes=(("TXT Files","*.txt"),("All Files","*.*")))

I use this in many projects, you can add arguments such as initialdir, and you can specify allowable filetypes!

Dylan Logan
  • 395
  • 7
  • 18