简体   繁体   中英

Python Tkinter “wait_window()” never continuing

I have a Tkinter app in python and I am trying to use a custom-defined dialog box in it. So I have a main App class and a Dialog class that extends Toplevel . Within the body of the App class a Dialog needs to pop up and allow the user to enter information which can then be passed to the main App .

I know that I need to use the wait_window() method so that the main app waits for the dialog to be destroyed before continuing in the code. But for some reason even after I exit the dialog box the program doesn't continue.

I have code that looks like

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)

When I run the app, the dialog box appears as intended, and works as intended. However, when I press the red X in the corner, the program does not continue. The dialog box closes and "Destroying" is printed so I know the destroy method is being called, but "Continuing" is not printed, nor is any of the data being used.

I tried adding a button to the dialog that calls self.destroy as its command to explicitly destroy it. I also tried not overriding the destroy method in Dialog to see if that was causing it but none of this worked.

I also treid doing dialog.wait_window() instead of self.root.wait_window(dialog) to see if that helped but it didn't.

Any help is much appreciated. If more code is needed I can provide it. Thank you in advance.

Do not call mainloop() a second time. mainloop() should always be called exactly once for the life of the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM