HOWTO · Tkinter
Come chiudere una finestra Tkinter con un pulsante
Questo tutorial introduce come chiudere una finestra di Tkinter con un pulsante che ha una funzione o un comando collegato ad essa
In questa pagina
Possiamo usare una funzione o un comando collegato ad un pulsante nell’interfaccia grafica di Tkinter per chiudere la finestra di Tkinter quando l’utente fa clic su di essa.
root.destroy() Metodo della classe per chiudere la finestra di Tkinter
try:
import Tkinter as tk
except:
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("100x50")
button = tk.Button(self.root, text="Click and Quit", command=self.quit)
button.pack()
self.root.mainloop()
def quit(self):
self.root.destroy()
app = Test()
destroy() distrugge o chiude la finestra.
destroy() Metodo non di classe per chiudere la finestra di Tkinter
try:
import Tkinter as tk
except:
import tkinter as tk
root = tk.Tk()
root.geometry("100x50")
def close_window():
root.destroy()
button = tk.Button(text="Click and Quit", command=close_window)
button.pack()
root.mainloop()
Associare la funzione root.destroy all’attributo command del pulsante direttamente
Potremmo legare direttamente la funzione root.destroy all’attributo command del pulsante senza definire più la funzione extra close_window.
try:
import Tkinter as tk
except:
import tkinter as tk
root = tk.Tk()
root.geometry("100x50")
button = tk.Button(text="Click and Quit", command=root.destroy)
button.pack()
root.mainloop()
root.quit per chiudere la finestra di Tkinter
root.quit esce non solo dalla Finestra Tkinter, ma più precisamente dall’intero interprete di Tcl.
Potrebbe essere usato se la vostra applicazione Tkinter non viene avviata da Python idle. Non è raccomandato l’uso di root.quit se la vostra applicazione Tkinter viene chiamata da idle perché quit non solo ucciderà la vostra applicazione Tkinter ma anche l’idle perché idle è anche un’applicazione Tkinter.
try:
import Tkinter as tk
except:
import tkinter as tk
root = tk.Tk()
root.geometry("100x50")
button = tk.Button(text="Click and Quit", command=root.quit)
button.pack()
root.mainloop()