How do you close a python - tkinter window, when the window object is encapsulated?

OmniOrus

Ok, so this is from a larger project that I am working on, so I apologize if it looks messy.

The issue is that when I click the 'Exit Program' Button on the GUI, the window remains active. I know that the button is working as when I hit the 'x' on the top right corner the window; the program closes, so the run variable has been set back to 0, which stops the code from looping.

My question is how do I get the window to be closed automatically when the exit button is clicked, because the root.destroy() method isn't doing it.

#imports
from tkinter import *
import random, pickle, shelve

#global vars
run = 0

class Window(Frame):
#the class that manages the UI window

    def __init__(self, master, screen_type = 0):
        """Initilize the frame"""
        super(Window, self).__init__(master)
        self.grid()
        if screen_type == 1:
            self.log_in_screen()

    def log_in_screen(self):
        #Program Exit Button
        self.exit = Button(self, text = " Exit Program ", command = self.end)
        self.exit.grid(row = 3, column = 0, columnspan = 2, sticky = W)

    def end(self):
        global run, root
        run = 0
        root.destroy()

#Main Loop
def main():
    global run, root
    run = 1
    while run != 0:
        root = Tk()
        root.title("Budget Manager - 0.6.1")
        root.geometry("400x120")
        screen = Window(root, screen_type = run)
        root.mainloop()

store = shelve.open("store.dat", "c")
main()
store.close()
Bryan Oakley

My question is how do I get the window to be closed automatically when the exit button is clicked, because the root.destroy() method isn't doing it.

The answer is: call destroy() on the root window. You say it isn't working, but the code you posted seems to work, and what destroy() is documented to do is exactly what you describe you want to have happen: it will destroy the window. Your code creates new toplevel windows in a loop, so maybe it only appears to not work since the old window id destroyed and the new window is created in the blink of an eye.

It seems like what you're really asking is "how can I make clicking on the "x" do the same as clicking on the "Exit program" button?". If that is the case, the answer is very straight-forward, even with your unconventional code that creates root windows in a loop.

To get the "x" button on the window frame to call a function instead of destroying the window, use the wm_protocol method with the "WM_DELETE_WINDOW" constant and the function you want it to call.

For example:

while run != 0:
    root = Tk()
    ...
    screen = Window(root, screen_type = run)
    root.wm_protocol("WM_DELETE_WINDOW", screen.end)
    ...
    root.mainloop()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python tkinter 8.5 - How do you change focus from main window to a pop up window?

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 open and close another window with scrollbar in tkinter for python 3.5.?

From Dev

How to close tkinter window without a button and not closing Python completely?

From Dev

How do you make an animation with the title of a Tkinter window?

From Dev

exit python program without close Tkinter window

From Dev

How to close tkinter window without a button?

From Dev

python tkinter open new window with button click and close first window

From Dev

how to do a "Save as" window with tkinter?

From Dev

Python Tkinter: How do I apply a new background image when opening a new tk window?

From Dev

how do i display search results in Python Tkinter window

From Dev

How to prevent tkinter window from closing when I close Windows command console

From Dev

How do you make a basic fullscreen window in Python?

From Dev

Only close one tkinter window

From Dev

How to close current window (in Code) when launching new Window

From Dev

How to open a window and close another window when a button is pressed with javascript?

From Dev

How do I close a window by clicking on it?

From Dev

How do I close login request 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

How do you close 1 window of a gui that has 2 windows open without closing both?

From Dev

How to get the "Leave site? Changes that you made may not be saved" when trying to close a window with a populated form

From Dev

Python Tkinter - Close dialog without closing main window

From Dev

How to close IDLE window which is opened by Tkinter.Tk()

From Dev

How to close pop up window, when there is no XPath for close button?

From Java

How to close a pyglet window?

From Dev

How to close window OSX?

From Dev

How to close Window Switcher

From Dev

How to close window at byobu?

Related Related

  1. 1

    Python tkinter 8.5 - How do you change focus from main window to a pop up window?

  2. 2

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

  3. 3

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

  4. 4

    How to open and close another window with scrollbar in tkinter for python 3.5.?

  5. 5

    How to close tkinter window without a button and not closing Python completely?

  6. 6

    How do you make an animation with the title of a Tkinter window?

  7. 7

    exit python program without close Tkinter window

  8. 8

    How to close tkinter window without a button?

  9. 9

    python tkinter open new window with button click and close first window

  10. 10

    how to do a "Save as" window with tkinter?

  11. 11

    Python Tkinter: How do I apply a new background image when opening a new tk window?

  12. 12

    how do i display search results in Python Tkinter window

  13. 13

    How to prevent tkinter window from closing when I close Windows command console

  14. 14

    How do you make a basic fullscreen window in Python?

  15. 15

    Only close one tkinter window

  16. 16

    How to close current window (in Code) when launching new Window

  17. 17

    How to open a window and close another window when a button is pressed with javascript?

  18. 18

    How do I close a window by clicking on it?

  19. 19

    How do I close login request window?

  20. 20

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

  21. 21

    How do you close 1 window of a gui that has 2 windows open without closing both?

  22. 22

    How to get the "Leave site? Changes that you made may not be saved" when trying to close a window with a populated form

  23. 23

    Python Tkinter - Close dialog without closing main window

  24. 24

    How to close IDLE window which is opened by Tkinter.Tk()

  25. 25

    How to close pop up window, when there is no XPath for close button?

  26. 26

    How to close a pyglet window?

  27. 27

    How to close window OSX?

  28. 28

    How to close Window Switcher

  29. 29

    How to close window at byobu?

HotTag

Archive