Tkinter. How to destroy the root window while top level window is waiting for a variable

I_do_python

I have an issue whereby a user can open a top_level window which sits waiting for a variable via the wait_variable() method. But while the top_level window is open, the root window is still visible and the user is able to close the root window via usual methods (this is intentional). What I'd like (and sort of expect tkinter to do) is that calling .destroy() or .quit() on the root window will cause all of root's children to be terminated. But what appears to be happening is that the top_level window remains stuck in its local event loop and can only be killed via the task manager once its parent has gone.

So what am I doing wrong? How do I get the top_level window to listen out for it's parents destruction while its in a local event loop?

.

Below is some example code that demonstrates the issue. If you run the code and follow the steps below your IDE WILL CRASH!! probably, so save you work. Push the Click button in root. The top level window will appear with another button that says Test. Now close the root window. The IDE will hang.

import tkinter
root = tkinter.Tk()
def toplevel(event=None):
    def set1(event=None):
        vr.set(1)
    tp = tkinter.Toplevel(root)
    vr = tkinter.IntVar()
    bt_ = tkinter.Button(tp,text='Test',command=set1)
    bt_.grid()
    tp.wait_variable(vr)
    tp.destroy()
bt = tkinter.Button(root,text='Click',command=toplevel)
bt.grid()
root.mainloop()

Edit: My current solution is to repoint the WM_DELETE_WINDOW protocol when the top_level window starts, to a function that sets the variable that the local event loop is waiting for, and then destroys the root window.

Bryan Oakley

The correct solution IMO is to not wait on the variable, but rather, wait on the window. Your button then must take on the responsibility of destroying the window instead of or in addition to setting the variable.

This is the right solution for another reason: as written your program will also hang if the user destroys the toplevel with the buttons in the titlebar. Since you're waiting on a variable, because the window is destroyed the variable will never be set.

(the solution you proposed in your edit is also valid -- register with WM_DELETE_WINDOW on the toplevel to set the variable when the window is destroyed. The effect will be the same).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Tkinter. How to destroy the root window while top level window is waiting for a variable

From Dev

Tkinter; how to use scrollbar in a popup/top-level window that opens when a button is pressed in main root window

From Dev

Tkinter root_window.destroy() error

From Dev

Hiding Tkinter root window while showing modal window

From Dev

Top level window in WinForms

From Dev

destroy a tkinter window an proceed further

From Dev

Tkinter grid manager can't manage 'top level window' menu

From Dev

Tkinter grid manager can't manage 'top level window' menu

From Dev

How to update a window in Tkinter while using a for loop

From Dev

Top-level window size

From Dev

Top-level window size

From Dev

Python Variable in tkinter window

From Dev

Tkinter window title as a variable

From Dev

Python Tkinter: How do you create a toplevel window and destroy the previous window?

From Dev

Python Tkinter: How do you create a toplevel window and destroy the previous window?

From Dev

How to destroy a parent window when child window's red X is clicked in Tkinter 8.5 and Python 3.3

From Dev

Tkinter root window not coming up

From Dev

Tkinter Toplevel : Destroy window when not focused

From Dev

Python Tkinter destroy toplevel leaves window open

From Dev

How to create a draggable (borderless and titleless) top level window in QT

From Dev

How to add a question mark [?] button on the top of a tkinter window

From Dev

How to clear window with tkinter

From Dev

How to prevent image to fill entire Tkinter window while resizing?

From Dev

C# Finding top-level window

From Dev

Change the title for the default top level window

From Dev

Popup window unable to receive focus when top level window is minimized

From Dev

Determine if GTK window in xlib is a subwindow or a top level window

From Dev

How can I destroy a window (using tkinter) after a button in it was clicked several times?

From Dev

How can I destroy a window (using tkinter) after a button in it was clicked several times?

Related Related

  1. 1

    Tkinter. How to destroy the root window while top level window is waiting for a variable

  2. 2

    Tkinter; how to use scrollbar in a popup/top-level window that opens when a button is pressed in main root window

  3. 3

    Tkinter root_window.destroy() error

  4. 4

    Hiding Tkinter root window while showing modal window

  5. 5

    Top level window in WinForms

  6. 6

    destroy a tkinter window an proceed further

  7. 7

    Tkinter grid manager can't manage 'top level window' menu

  8. 8

    Tkinter grid manager can't manage 'top level window' menu

  9. 9

    How to update a window in Tkinter while using a for loop

  10. 10

    Top-level window size

  11. 11

    Top-level window size

  12. 12

    Python Variable in tkinter window

  13. 13

    Tkinter window title as a variable

  14. 14

    Python Tkinter: How do you create a toplevel window and destroy the previous window?

  15. 15

    Python Tkinter: How do you create a toplevel window and destroy the previous window?

  16. 16

    How to destroy a parent window when child window's red X is clicked in Tkinter 8.5 and Python 3.3

  17. 17

    Tkinter root window not coming up

  18. 18

    Tkinter Toplevel : Destroy window when not focused

  19. 19

    Python Tkinter destroy toplevel leaves window open

  20. 20

    How to create a draggable (borderless and titleless) top level window in QT

  21. 21

    How to add a question mark [?] button on the top of a tkinter window

  22. 22

    How to clear window with tkinter

  23. 23

    How to prevent image to fill entire Tkinter window while resizing?

  24. 24

    C# Finding top-level window

  25. 25

    Change the title for the default top level window

  26. 26

    Popup window unable to receive focus when top level window is minimized

  27. 27

    Determine if GTK window in xlib is a subwindow or a top level window

  28. 28

    How can I destroy a window (using tkinter) after a button in it was clicked several times?

  29. 29

    How can I destroy a window (using tkinter) after a button in it was clicked several times?

HotTag

Archive