I can do the following in my program to get a simple open file dialog and print the selected file path. Unfortunately it doesn't go away right away when the user selects the file, and stays around for over 5 minutes. How do I make the window disappear immediately once a selection has been made before executing more python code? After the Tkinter code I do try to import some video using OpenCV which I think may be causing the slowing. My OpenCV code does execute properly and I don't think there is a problem with that alone (i.e. some interaction is causing the error & maybe some intensive process is started before Tkinter wraps up its GUI dialog).
import Tkinter as Tk
import cv2
from tkFileDialog import askopenfilename
root = Tk.Tk()
root.withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
cap = cv2.VideoCapture('video.mp4') # this works just fine
I am using Python 2.7 and Mac OS X 10.9 if that is useful.
[EDIT: This does not seem to be a problem for all, but it is for me, so I am changing the question to also include debugging the problem. I don't want anything to execute until the Tkinter open file dialog window is done closing in the GUI. It seems that a subsequent step in my program (an open cv video import) could somehow be causing Tkinter to slow things down, so I want to ensure it does close before any new process is started. Again, the Tkinter window does actually close after 5 minutes...]
I was having some trouble with Tkinter dialog boxes. Like you, the dialog just sat there after I had selected a file. I didn't try leaving it for very long, it may have disappeared after 5 minutes as you said your's did. After lots of random experimentation I found that calling root.update()
before the askopenfilename()
line seemed to fix it.
For reference, this is the code I was testing with:
import sys
from tkinter import *
from tkinter import filedialog
#instantiate a Tk window
root = Tk()
#set the title of the window
root.title('Tk test')
# I don't know, what this does, but it fixes askopenfilename if I use it.
root.update()
print(filedialog.askopenfilename(title='dialogue? surely.'))
Exactly the problem I had - sometimes the file dialog would go away after a while, sometimes not. But it always seemed to block a later status windows. Adding the root.update() fixed both problems immediately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With