如何在tkinter上关闭上一个窗口? [英] How to close previous window on tkinter?

查看:959
本文介绍了如何在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()

如您所见,我正在尝试使用.destroy(),但是它没有用.有什么办法吗?我刚刚开始学习Python,所以我知道这可能很简单.感谢您的帮助!

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!

推荐答案

我看到了几个问题.主要的原因是您不能调用newwindow.destroy(),因为newwindow是一个函数,而不是tk.Toplevel小部件.另一个是janela1.destroy()自我毁灭,它是根窗口.

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.

您可以withdraw()代替它们来破坏窗户.我认为这是您想要的代码:

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()

即使不是严格必要的,我还是做了一些改变,那就是如何将调用place()的结果分配给小部件的名称.由于place()(以及pack()grid())始终返回None,因此该变量最终将获得的值-这永远是您想要的.您将在这里摆脱它,但这仅是因为这些名称不再被引用.

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.

这篇关于如何在tkinter上关闭上一个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆