如何在被调用之前防止

如何在被调用之前防止

本文介绍了如何在被调用之前防止 Tkinter 窗口打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行这个脚本时,会出现两个窗口,一个是文件选择窗口,一个是 Tkinter 窗口.如何更改此设置以便 Tkinter 窗口仅在选择文件后打开?谢谢

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 selected? Thanks

def main():
    my_file = askopenfilename()
    stage1()

def stage1():
    master = Tk()
    master.mainloop()

推荐答案

The window master 只有在文件对话框关闭后才打开(尝试更改其标题以检查),您看到的第一个窗口是文件对话框的父窗口.实际上,tkinter 文件对话框是顶级窗口,因此如果没有父窗口,它们就无法存在.所以你看到的第一个窗口是文件对话框的父窗口.

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.

但是可以使用 withdraw 方法隐藏父窗口,然后使用 deiconify 恢复:

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()

这篇关于如何在被调用之前防止 Tkinter 窗口打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:09