问题
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():
newwindow.destroy()
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).place(x=110, y=10)
tk.Button(newwindow2, text='Fim').place(x=250, y=470)
def newwindow():
janela1.destroy()
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).place(x=110, y=10)
tk.Button(newwindow, text='Próximo', command=newwindow2).place(x=250, y=470)
janela1 = tk.Tk()
janela1.title('Nível da grama região 1')
janela1.geometry("580x520")
labl1=Label(janela1, text='A foto do nível da grama na região 1 foi tirada: ',font=30).place(x=110, y=10)
tk.Button(janela1, text='Próximo', command=newwindow).place(x=250, y=470)
janela1.mainloop()
As you can see I'm trying to use .destroy() but it's not working. Any solutions? I'm just starting to learn Python so I'm aware this might be very simple. Thanks for the help!
回答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.
来源:https://stackoverflow.com/questions/58721295/how-to-close-previous-window-on-tkinter