Home > database >  Tkinter askdirectory() not showing on screen
Tkinter askdirectory() not showing on screen

Time:10-16

I am trying to allow a user to choose the directory where they want to save a file to, however anytime I call a Tkinkter function AFTER the initial askopenfilename(), it just hangs. askopenfilename() works the first time it's called but that and askdirectory() won't work anytime after. This is just me messing around with Tkinter as I'm new to it. The Pandas stuff all works fine and no error is being shown; Tkinter just hangs after it reaches line 21 (saved = askdirectory()).

from tkinter import Tk
from tkinter.filedialog import askdirectory, askopenfile, askopenfilename

excelArray = ["excel", "xlsx", "xl", "x", "exsel", "ex", "e"]
consoleArray = ["console", "c", "concole", "con", "consle"]

root=Tk()
root.withdraw()

pathToExcel = askopenfilename()
print(f"\nOpening the excel document located at: {pathToExcel}")

userOutput = input("\nWhere would you like the output to be? Console or Excel?").lower()
if userOutput in excelArray:
    print("Where would you like to save it?")
    saved = askdirectory()
    print("Saved")
else:
    print("\n")
    print(test[['1', '2', '3']])

CodePudding user response:

For some reason, the program hangs when the root is withdrawn. From your code, remove the following things:

from tkinter import Tk
root = Tk()
root.withdraw()

So the final code will be:

from tkinter.filedialog import askdirectory, askopenfile, askopenfilename

excelArray = ["excel", "xlsx", "xl", "x", "exsel", "ex", "e"]
consoleArray = ["console", "c", "concole", "con", "consle"]

pathToExcel = askopenfilename()
print(f"\nOpening the excel document located at: {pathToExcel}")

userOutput = input("\nWhere would you like the output to be? Console or Excel?").lower()
if userOutput in excelArray:
    print("Where would you like to save it?")
    saved = askdirectory()
    print("Saved")
else:
    print("\n")
    print(test[['1', '2', '3']])
  • Related