Python Forum
[Tkinter] File Chooser will not close
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] File Chooser will not close
#1
Good Evening,

I just learned about the existence of tkinter as I was trying to create a dialog box to allow users to select file, and then import the file in pandas df. I saw plenty of examples online and tutorial on youtube regarding how to create it using the askopenfilename function within tkinter. I got the program to run and return the absolute file path correctly the problem am having and cant seem to find the answer to is that once I close the dialog box after selecting the file. My output windows continues to print the following " r\n\r\n" indefinitely. Ive tried adding .mainloop(), .destroy(), and .quit() after the askopenfile function and nothing seems to work. Any help in understanding what is going on will be greatly appreciate it.

Code:


import  tkinter as tk
from tkinter import filedialog

root = tk.Tk()

file = filedialog.askopenfilename()

root.mainloop()
Gribouillis write Aug-04-2023, 05:20 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Tkinter needs a mainloop() to work and dialog boxes must be used while the mainloop is running. If you just want to pick a file without mainloop, you could use easygui (among other alternatives)
from easygui import fileopenbox

result = fileopenbox('Spam spam spam')

print(result)
Reply
#3
The tkinter dialogs are written to use their own mainloop, and even initialize the tkinter application, if there is no root window and no mainloop. For example, this code will create a file dialog and return the selected filename.
from tkinter import filedialog

print(filedialog.askopenfilename())
Reply
#4
(Aug-04-2023, 11:40 AM)deanhystad Wrote: The tkinter dialogs are written to use their own mainloop, and even initialize the tkinter application, if there is no root window and no mainloop. For example, this code will create a file dialog and return the selected filename.
from tkinter import filedialog

print(filedialog.askopenfilename())

Hello and thank you for the quick response. I read something like online somewhere so that why I wrote the original code and it works to the point that I do get the path to pass into a variable, the problem is that no matter what I have tried my output window starts self generating "r\n\r\n" and wont let me execute any further code. And when I click the "X" on the self created window that appears behind the dialog box the program crashes my session of python on Sypder. My code is very simple as I have not been able to get passed this point. But eventually the goal is to pass the variable "path" string into pandas to create the a df.

[Code:]

import tkinter as tk
from tkinter import filedialog

path = filedialog.askopen()

[end code]

output window looks like this:

In [2] path = filedialog.askopenfilename()

r\n\r\n
r\n\r\n
r\n\r\n
r\n\r\n
r\n\r\n
Reply
#5
(Aug-04-2023, 05:29 AM)Gribouillis Wrote: Tkinter needs a mainloop() to work and dialog boxes must be used while the mainloop is running. If you just want to pick a file without mainloop, you could use easygui (among other alternatives)
from easygui import fileopenbox

result = fileopenbox('Spam spam spam')

print(result)

Thank you for that suggestion unfortunately my system is locked and I cant import easygui, as weird as that sounds.
Reply
#6
This program contains an error
import tkinter as tk
from tkinter import filedialog

path = filedialog.askopen()
Error:
AttributeError: module 'tkinter.filedialog' has no attribute 'askopen'
There is not a filedialog.askopen() function

I don't use spyder, so I don't know if it works well with tkinter. I do know IDLE does not play well with tkinter. Mostly works, but not really.

Try running your program from the command line to see what happens.

What Python are you using? What version of Spyder?
Reply
#7
(Aug-04-2023, 07:11 PM)deanhystad Wrote: This program contains an error
import tkinter as tk
from tkinter import filedialog

path = filedialog.askopen()
Error:
AttributeError: module 'tkinter.filedialog' has no attribute 'askopen'
There is not a filedialog.askopen() function

I don't use spyder, so I don't know if it works well with tkinter. I do know IDLE does not play well with tkinter. Mostly works, but not really.

Try running your program from the command line to see what happens.

What Python are you using? What version of Spyder?

Am sorry for that I accidently left the rest of the function name out. Please see below for modified code and output.

[Code:]

import tkinter as tk
from tkinter import filedialog

path = filedialog.askopenfilename()
[end code]

output window looks like this:

In [2] path = filedialog.askopenfilename()

r\n\r\n
r\n\r\n
r\n\r\n
r\n\r\n
r\n\r\n
...
[End output]

The code works in the sense that it does open a file window, I can select the file I want and it stores it into the "path" variable correctly. My problem is that it starts generating the "r\n\r\n" and it wont let me process any more code. As if something loop is still running, the kernel indicator shows a busy status.

Regarding the version info I am running Spyder IDE 5.2.2 with Py 3.9.13
deanhystad write Aug-07-2023, 02:23 PM:
Use Python tags, not Code tags. I don't know why there are code tags. They don't do anything.
Reply
#8
I don't understand what this means:
Quote:In [2] path = filedialog.askopenfilename()
What is the "In [2]"?

Have you run you code from the command line?
Reply
#9
(Aug-07-2023, 02:25 PM)deanhystad Wrote: I don't understand what this means:
Quote:In [2] path = filedialog.askopenfilename()
What is the "In [2]"?

Have you run you code from the command line?

sorry the In[2] is just the line number in my output window. So when I execute code the output line is displayed as " ln [number]". I just copied the output directly as it appeared in the window. But all am doing here is creating the variable "path" and assigning it to the value that is extracted using the "filedialog.askopenfilename()" function. In this case it would be something like "C:/user/documents/test.xlsx".
Reply
#10
What happens if you run the python program from the command line?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020