我想销毁一个窗口然后重新打开它(tkinter) [英] i want to destroy a window and then re open it (tkinter)

查看:35
本文介绍了我想销毁一个窗口然后重新打开它(tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的主窗口打开一个窗口,然后能够从我创建的窗口中销毁并再次打开原始窗口

I want to open a window from my main window and then be able to destroy and open the original window again from the window I created

到目前为止,这是我的代码,但我刚刚得到:tkinter.TclError:无法调用wm"命令:应用程序已被破坏

Here is my code so far but i just get: tkinter.TclError: can't invoke "wm" command: application has been destroyed

如果有任何帮助来解决这个问题,我们将不胜感激:)

Any help to fix this would be much appreciated :)

这是我的代码:

    from tkinter import*

    root = Tk()
    root.title("Using Frames")
    root.geometry("400x600")

    frame = LabelFrame(root, text="pages",
    padx=5,pady=5)
    frame.pack(padx=10,pady=10)
    def create_window():
        window1 = Tk()
        btn = Button(window1,text="destroy main page",command=root.destroy)
        btn.pack()
        btn2 = Button(window1,text="open main page",command=root.deiconify)
        btn2.pack()
        window1.mainloop()

    b1 = Button(frame,text="create window 2",command=create_window)
    b1.pack()
    root.mainloop()

推荐答案

销毁此窗口后,除非创建新窗口,否则无法再次打开它.

After you destroy this window,you couldn't open it again unless you create a new window.

您应该使用 .withdraw().deiconify() 使其隐藏或显示.

You should use .withdraw() and .deiconify() to make it hide or show.

此代码可能会解决您的问题:

This code maybe solve your problem:

from tkinter import *

root = Tk()
root.title("Using Frames")
root.geometry("400x600")

frame = LabelFrame(root, text="pages",
                   padx=5, pady=5)
frame.pack(padx=10, pady=10)


def create_window():
    window1 = Toplevel()
    btn = Button(window1, text="destroy main page", command=root.withdraw)
    btn.pack()
    btn2 = Button(window1, text="open main page", command=root.deiconify)
    btn2.pack()
    window1.mainloop()


b1 = Button(frame, text="create window 2", command=create_window)
b1.pack()
root.mainloop()

这篇关于我想销毁一个窗口然后重新打开它(tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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