Python Tkinter "wait_window()" 永远不会继续
Python Tkinter "wait_window()" never continuing
我在 python 中有一个 Tkinter 应用程序,我正在尝试在其中使用自定义对话框。所以我有一个主 App
class 和一个扩展 Toplevel
的 Dialog
class。在 App
class 的正文中,需要弹出一个 Dialog
并允许用户输入信息,然后可以将这些信息传递给主 App
。
我知道我需要使用 wait_window()
方法,以便主应用程序在继续代码之前等待对话框被销毁。但出于某种原因,即使我退出了对话框,程序也没有继续。
我的代码看起来像
class App:
def __init__(self):
self.root = Tk()
#more init code...
#more unrelated methods...
def analyze(self):
dialog = Dialog()
self.root.wait_window(dialog)
print("Continuing...")
#use the data collected in 'dialog'
class Dialog(Toplevel):
def __init__(self):
Toplevel.__init__(self)
#set up the components of the dialog
#all input data gets saved as instance variables so I can access it
self.mainloop()
def destroy(self):
print("Destroying")
Toplevel.destroy(self)
当我 运行 应用程序时,对话框按预期显示,并按预期工作。但是,当我按下角落里的红色 X 时,程序不会继续。对话框关闭并打印 "Destroying",因此我知道正在调用 destroy
方法,但未打印 "Continuing",也未使用任何数据。
我尝试在对话框中添加一个按钮,调用 self.destroy
作为明确销毁它的命令。我还尝试不覆盖 Dialog
中的 destroy
方法以查看是否是它造成的,但是 none 有效。
我也试着做 dialog.wait_window()
而不是 self.root.wait_window(dialog)
看看是否有帮助,但没有。
非常感谢任何帮助。如果需要更多代码,我可以提供。提前谢谢你。
请勿再次调用 mainloop()
。 mainloop()
应该在程序的整个生命周期内只被调用一次。
我在 python 中有一个 Tkinter 应用程序,我正在尝试在其中使用自定义对话框。所以我有一个主 App
class 和一个扩展 Toplevel
的 Dialog
class。在 App
class 的正文中,需要弹出一个 Dialog
并允许用户输入信息,然后可以将这些信息传递给主 App
。
我知道我需要使用 wait_window()
方法,以便主应用程序在继续代码之前等待对话框被销毁。但出于某种原因,即使我退出了对话框,程序也没有继续。
我的代码看起来像
class App:
def __init__(self):
self.root = Tk()
#more init code...
#more unrelated methods...
def analyze(self):
dialog = Dialog()
self.root.wait_window(dialog)
print("Continuing...")
#use the data collected in 'dialog'
class Dialog(Toplevel):
def __init__(self):
Toplevel.__init__(self)
#set up the components of the dialog
#all input data gets saved as instance variables so I can access it
self.mainloop()
def destroy(self):
print("Destroying")
Toplevel.destroy(self)
当我 运行 应用程序时,对话框按预期显示,并按预期工作。但是,当我按下角落里的红色 X 时,程序不会继续。对话框关闭并打印 "Destroying",因此我知道正在调用 destroy
方法,但未打印 "Continuing",也未使用任何数据。
我尝试在对话框中添加一个按钮,调用 self.destroy
作为明确销毁它的命令。我还尝试不覆盖 Dialog
中的 destroy
方法以查看是否是它造成的,但是 none 有效。
我也试着做 dialog.wait_window()
而不是 self.root.wait_window(dialog)
看看是否有帮助,但没有。
非常感谢任何帮助。如果需要更多代码,我可以提供。提前谢谢你。
请勿再次调用 mainloop()
。 mainloop()
应该在程序的整个生命周期内只被调用一次。