python - Tkinter window not closing after closed file dialog -
i close file open dialog after selecting file. code, can select file file open dialog remains open until click 'x'. how can close window after have selected file.
here code:
import sys tkinter import * tkinter.filedialog import askopenfilename fname = "unassigned" def openfile(): global fname fname = askopenfilename() if __name__ == '__main__': b = button(text='file open', command = openfile).pack(fill=x) mainloop() print (fname)
the file dialog closing fine. think trying tkinter window created hold button not closing after select file dialog. have this, need restructure program bit.
first, need explicitly create tk
window hold button:
root = tk()
you should list window button's parent:
button(root, text='file open', command = openfile).pack(fill=x) # ^^^^
finally, should call destroy
method of root
window @ end of openfile
:
root.destroy()
this cause window close , tkinter mainloop exit.
in all, script this:
import sys tkinter import * tkinter.filedialog import askopenfilename fname = "unassigned" def openfile(): global fname fname = askopenfilename() root.destroy() if __name__ == '__main__': root = tk() button(root, text='file open', command = openfile).pack(fill=x) mainloop() print (fname)
Comments
Post a Comment