Skip to content Skip to sidebar Skip to footer

How To Close Previous Window On Tkinter?

I'm trying to close the previous window when I click the button to go to the next window. I'm not being able to do it. What's wrong? from tkinter import * def newwindow2(): ne

Solution 1:

I see see several problems. The main one being that you can't call newwindow.destroy() because newwindow is a function not a tk.Toplevel widget. Another is the janela1.destroy() destroying itself, and it's the root window.

Instead of destroying windows, you can just withdraw() them. Here's code that I think does what you want:

from tkinter import *
import tkinter as tk

def make_newwindow2():
#    newwindow.destroy()
    global newwindow2

    newwindow.withdraw()
    newwindow2 = tk.Toplevel()
    newwindow2.title('Nível da grama região 3')
    newwindow2.geometry('580x520')
    labl3 = Label(newwindow2,
                  text='A foto do nível da grama na região 3 foi tirada:\n', font=30)
    labl3.place(x=110, y=10)
    tk.Button(newwindow2, text='Fim', command=root.quit).place(x=250, y=470)

def make_newwindow():
#    janela1.destroy()
    global newwindow

    root.withdraw()
    newwindow = tk.Toplevel()
    newwindow.title('Nível da grama região 2')
    newwindow.geometry('580x520')
    labl2 = Label(newwindow,
                  text='A foto do nível da grama na região 2 foi tirada:\n', font=30)
    labl2.place(x=110, y=10)
    tk.Button(newwindow, text='Próximo', command=make_newwindow2).place(x=250, y=470)

root = tk.Tk()
root.title('Nível da grama região 1')
root.geometry("580x520")

labl1 = Label(root, text='A foto do nível da grama na região 1 foi tirada: ', font=30)
labl1.place(x=110, y=10)
tk.Button(root, text='Próximo', command=make_newwindow).place(x=250, y=470)

root.mainloop()

Something else I changed, even though it wasn't strictly necessary, was how you were assigning the result of calling place() to the names of a widgets. Since place() (and pack() and grid()) always return None, that's the value the variable will end up with — which is never what you'd want. You're getting away with it here, but only because those names aren't referenced again.


Post a Comment for "How To Close Previous Window On Tkinter?"