python tkinter 关闭窗口_如何在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():

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!

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值