Python close all windows

closing all windows in python tkinter

You are importing tkinter 2 times here. Onces with * and ones as tk.

this will help you avoid overriding anything that other libraries imports or having tkinter’s functions overwritten by other imports.

You have a very unorthodox way of creating your tkinter app but if you wish to keep everything as is here is what you need to change:

Lets remove the cancel button from your quitDialog window and then add a yes and no button. This will server to allow you to either say yes to destroy all windows or to say no to only destroy the quitDialog window.

First we need to add an arguement to your QuitDialog class so we can pass the any window or frame we want to it.

So add the instance argument to your QuitDialog() class like below:

class QuitDialog(): def __init__(self, instance): self.instance = instance 
cancelButton = tk.Button(master= self.quitDialog , text='Cancel', command = self.quitALL).grid(row=2, column=1) 
quitButton = tk.Button(master= self.quitDialog , text='Yes', command = self.quitALL).grid(row=2, column=1) cancelButton = tk.Button(master= self.quitDialog, text='No', command = lambda: self.quitDialog.destroy()).grid(row=2, column=2) 

Then lets change your quitALL() method to:

def quitALL(self): self.quitDialog.destroy() self.instance.destroy() 

This will use our instance argument to destroy a window we pass in.

Читайте также:  Python запуск скрипта pycharm

Now change your showQuitDialog() method to:

def showQuitDialog(self): quitdialog = QuitDialog(self.mainWindow) quitdialog.start() 

As you can see we are now passing the the tk window self.mainWindow to the QuitDialog class so we can decide on weather or not to close it.

Below is the copy past version of your code that should work as you need it to:

import tkinter as tk from tkinter import ttk import tkinter.messagebox import datetime class AlertDialog: def __init__(self): self.invalidDiag = tk.Toplevel() invalidInput = tk.Label(master=self.invalidDiag, text='Error: Invalid Input').grid(row=1, column=1) closeButton = tk.Button(master=self.invalidDiag, text='Close', command=self.invalidDiag.destroy).grid(row=2, column=1) def start(self): # self.invalidDiag.grab_set() #takes control over the dialog (makes it active) self.invalidDiag.wait_window() class QuitDialog(): def __init__(self, instance): self.instance = instance self.quitDialog = tk.Toplevel() warnMessage = tk.Label(master=self.quitDialog, text='Are you sure that you want to quit? ').grid(row=1, column=1, columnspan=2) quitButton = tk.Button(master= self.quitDialog , text='Yes', command = self.quitALL).grid(row=2, column=1) cancelButton = tk.Button(master= self.quitDialog, text='No', command = lambda: self.quitDialog.destroy()).grid(row=2, column=2) def start(self): # self.invalidDiag.grab_set() #takes control over the dialog (makes it active) self.quitDialog.wait_window() def quitALL(self): self.quitDialog.destroy() self.instance.destroy() class TimeConverter: def __init__(self): self.mainWindow = tk.Tk() self.mainWindow.title("Seconds Converter") self.results = tk.StringVar() self.inputSecs = tk.StringVar() secLabel = tk.Label(master=self.mainWindow, text="Seconds:").grid(row=0, sticky="W") resultLabel = tk.Label(master=self.mainWindow, text="Converted Time:\n(H:M:S)").grid(row=1, sticky="W") calcResults = tk.Label(master=self.mainWindow, background='light gray', width=20, textvariable=self.results, anchor="w").grid(row=1, column=1) secEntry = tk.Entry(master=self.mainWindow, width=24, textvariable=self.inputSecs).grid(row=0, column=1) calcButton = tk.Button(master=self.mainWindow, text='Calculate', command=self.SecondsToHours).grid(row=2, column=0, sticky="w") quitButton = tk.Button(master=self.mainWindow, text='Quit', command=self.showQuitDialog).grid(row=3, column=1, sticky="E") def invalidInputEntered(self): errorDiag = AlertDialog() errorDiag.start() def showQuitDialog(self): quitdialog = QuitDialog(self.mainWindow) quitdialog.start() def startDisplay(self) -> None: self.mainWindow.mainloop() def destroyit(self): self.mainWindow.destroy() def SecondsToHours(self): try: inputseconds = int(self.inputSecs.get()) seconds = int(inputseconds % 60) minutes = int(((inputseconds - seconds) / 60) % 60) hours = int((((inputseconds - seconds) / 60) - minutes) / 60) tempResults = str(hours) + ':' + str(minutes) + ':' + str(seconds) self.results.set(tempResults) return except ValueError: self.invalidInputEntered() #self.showQuitDialog() if __name__ == '__main__': TimeConverter().startDisplay() 

Mike — SMT 15527

Interesting question. As I know, you can also use just quit() in order to quit from program by closing everything.

Источник

How do I close all windows in Python?

How do I close all the open pyplot windows (Matplotlib)?

  1. plt.
  2. close() by itself closes the current figure.
  3. close(h), where h is a Figure instance, closes that figure.
  4. close(num) closes the figure with number=num.
  5. close(name), where name is a string, closes the figure with that label.

How do I handle the window close event in tkinter?

Tkinter provides a custom handler to close the window. It acts as a callback function that the user can run in order to close the window. To close the window using the handler, we can use the destroy() method. It closes the window abruptly after calling it in any function or any widget.

How do I use multiple windows in tkinter?

“how to create multiple windows in tkinter with button” Code…

  1. #import module needed.
  2. import tkinter as tk.
  3. #write the new window function which.
  4. #will be called when button pressed.
  5. def new_window():
  6. window = tk. Toplevel(root)
  7. canvas = tk. Canvas(window, height=HEIGHT, width=WIDTH)
  8. canvas. pack()

How do you close all plots in python?

  1. close() by itself closes the current figure.
  2. close(h) where h is a Figure instance, closes that figure.
  3. close(num) closes figure number num.
  4. close(name) where name is a string, closes figure with that label.
  5. close(‘all’) closes all the figure windows.

What does PLT close do?

The close() function in pyplot module of matplotlib library is used to close a figure window. Parameters: This method accept only one parameters. fig : This parameter accepts the following values: None: This value will close the current figure.

How do you check if a tkinter window has been closed?

“check for if the window is closed tkinter” Code Answer

  1. import tkinter as tk.
  2. from tkinter import messagebox.
  3. root = tk. Tk()
  4. def on_closing():
  5. if messagebox. askokcancel(“Quit”, “Do you want to quit?” ):
  6. root. destroy()

How do I use toplevel in Tkinter?

  1. from tkinter import *
  2. root = Tk()
  3. root.geometry(“200×200”)
  4. def open():
  5. top = Toplevel(root)
  6. top.mainloop()
  7. btn = Button(root, text = “open”, command = open)
  8. btn.place(x=75,y=50)

What is Tk toplevel?

The Toplevel widget is used to create and display the toplevel windows which are directly managed by the window manager. The toplevel widget is used when a python application needs to represent some extra information, pop-up, or the group of widgets on the new window. …

How to close all windows in Tkinter Stack Overflow?

Lets remove the cancel button from your quitDialog window and then add a yes and no button. This will server to allow you to either say yes to destroy all windows or to say no to only destroy the quitDialog window. First we need to add an arguement to your QuitDialog class so we can pass the any window or frame we want to it.

How to close a window in Python using a button?

You could use lambda or functools.partial and a function that accepts the name of the window to destroy, or you could use nested functions, etc. Thanks for contributing an answer to Stack Overflow!

How to close the toplevel window in Python?

Use the Python Tkinter , create a sub-panel (TopLevel) to show something and get user input, after user inputed, clicked the “EXIT” found the whole GUI (main panel) also destory. How to only close the toplevel window?

When to use command = root.destroy In Tkinter?

When you use command = root.destroy you pass the method to the Button without the parentheses because you want Button to store the method for future calling, not to call it immediately when the button is created. Bonus fact, the destroy function works on more than just the Tkinter window.

Источник

closing all windows in python tkinter

You are importing tkinter 2 times here. Onces with * and ones as tk.

this will help you avoid overriding anything that other libraries imports or having tkinter’s functions overwritten by other imports.

You have a very unorthodox way of creating your tkinter app but if you wish to keep everything as is here is what you need to change:

Lets remove the cancel button from your quitDialog window and then add a yes and no button. This will server to allow you to either say yes to destroy all windows or to say no to only destroy the quitDialog window.

First we need to add an arguement to your QuitDialog class so we can pass the any window or frame we want to it.

So add the instance argument to your QuitDialog() class like below:

class QuitDialog(): def __init__(self, instance): self.instance = instance 
cancelButton = tk.Button(master= self.quitDialog , text='Cancel', command = self.quitALL).grid(row=2, column=1) 
quitButton = tk.Button(master= self.quitDialog , text='Yes', command = self.quitALL).grid(row=2, column=1) cancelButton = tk.Button(master= self.quitDialog, text='No', command = lambda: self.quitDialog.destroy()).grid(row=2, column=2) 

Then lets change your quitALL() method to:

def quitALL(self): self.quitDialog.destroy() self.instance.destroy() 

This will use our instance argument to destroy a window we pass in.

Now change your showQuitDialog() method to:

def showQuitDialog(self): quitdialog = QuitDialog(self.mainWindow) quitdialog.start() 

As you can see we are now passing the the tk window self.mainWindow to the QuitDialog class so we can decide on weather or not to close it.

Below is the copy past version of your code that should work as you need it to:

import tkinter as tk from tkinter import ttk import tkinter.messagebox import datetime class AlertDialog: def __init__(self): self.invalidDiag = tk.Toplevel() invalidInput = tk.Label(master=self.invalidDiag, text='Error: Invalid Input').grid(row=1, column=1) closeButton = tk.Button(master=self.invalidDiag, text='Close', command=self.invalidDiag.destroy).grid(row=2, column=1) def start(self): # self.invalidDiag.grab_set() #takes control over the dialog (makes it active) self.invalidDiag.wait_window() class QuitDialog(): def __init__(self, instance): self.instance = instance self.quitDialog = tk.Toplevel() warnMessage = tk.Label(master=self.quitDialog, text='Are you sure that you want to quit? ').grid(row=1, column=1, columnspan=2) quitButton = tk.Button(master= self.quitDialog , text='Yes', command = self.quitALL).grid(row=2, column=1) cancelButton = tk.Button(master= self.quitDialog, text='No', command = lambda: self.quitDialog.destroy()).grid(row=2, column=2) def start(self): # self.invalidDiag.grab_set() #takes control over the dialog (makes it active) self.quitDialog.wait_window() def quitALL(self): self.quitDialog.destroy() self.instance.destroy() class TimeConverter: def __init__(self): self.mainWindow = tk.Tk() self.mainWindow.title("Seconds Converter") self.results = tk.StringVar() self.inputSecs = tk.StringVar() secLabel = tk.Label(master=self.mainWindow, text="Seconds:").grid(row=0, sticky="W") resultLabel = tk.Label(master=self.mainWindow, text="Converted Time:\n(H:M:S)").grid(row=1, sticky="W") calcResults = tk.Label(master=self.mainWindow, background='light gray', width=20, textvariable=self.results, anchor="w").grid(row=1, column=1) secEntry = tk.Entry(master=self.mainWindow, width=24, textvariable=self.inputSecs).grid(row=0, column=1) calcButton = tk.Button(master=self.mainWindow, text='Calculate', command=self.SecondsToHours).grid(row=2, column=0, sticky="w") quitButton = tk.Button(master=self.mainWindow, text='Quit', command=self.showQuitDialog).grid(row=3, column=1, sticky="E") def invalidInputEntered(self): errorDiag = AlertDialog() errorDiag.start() def showQuitDialog(self): quitdialog = QuitDialog(self.mainWindow) quitdialog.start() def startDisplay(self) -> None: self.mainWindow.mainloop() def destroyit(self): self.mainWindow.destroy() def SecondsToHours(self): try: inputseconds = int(self.inputSecs.get()) seconds = int(inputseconds % 60) minutes = int(((inputseconds - seconds) / 60) % 60) hours = int((((inputseconds - seconds) / 60) - minutes) / 60) tempResults = str(hours) + ':' + str(minutes) + ':' + str(seconds) self.results.set(tempResults) return except ValueError: self.invalidInputEntered() #self.showQuitDialog() if __name__ == '__main__': TimeConverter().startDisplay() 

Mike — SMT 14986

Interesting question. As I know, you can also use just quit() in order to quit from program by closing everything.

Источник

Оцените статью