How to prevent Tkinter window opening before being called?

后端 未结 1 2034
北恋
北恋 2021-01-29 07:51

When I run this script, two windows appear, one for the file selection and the Tkinter window. How can I change this so that the Tkinter window only opens after a file has been

相关标签:
1条回答
  • 2021-01-29 08:18

    The window master does open only after the file dialog closure (try to change its title to check), the first window you see is the parent window of the file dialog. Indeed, the tkinter file dialogs are toplevel windows, so they cannot exist without a parent window. So the first window you see is the parent window of the file dialog.

    The parent window can however be hidden using the withdraw method and then restored with deiconify:

    from tkinter import Tk
    from tkinter.filedialog import askopenfilename
    
    def main():
        master = Tk()
        master.withdraw()  # hide window
        my_file = askopenfilename(parent=master)
        master.deiconify()  # show window
        master.mainloop()
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
提交回复
热议问题