在 tkinter python 中打开新窗口时关闭当前窗口

问题描述 投票:0回答:2

我使用 tkinter 在 Python 中创建了一个程序。我为两个窗口创建了两个单独的类。我通过单击一个窗口的按钮打开另一个窗口。我希望当新窗口打开时另一个窗口应该关闭。我的代码是

from tkinter import Tk, Toplevel
from tkinter import *
def main():
    main_window = Tk()
    app = first(main_window)
    main_window.mainloop()


class first:
    def __init__(self, root):
        self.root = root
        self.root.title('First window')

        self.root.geometry('1350x700+0+0')
        frame1 = Frame(self.root, bg='black')
        frame1.place(x=400, y=50, width=400, height=600)
        btn_1 = Button(frame1, command=self.second_window, text='open second window', font=("Times New Roman", 15, 'bold'), bd=3,
                           relief=RIDGE,
                           cursor='hand2', bg='red', fg='white', activeforeground='white', activebackground='red')
        btn_1.place(x=100, y=350, width=220, height=35)

    def second_window(self):
        self.new_window = Toplevel(self.root)
        self.app = second(self.new_window)


class second:
    def __init__(self, root):
        self.root = root
        self.root.title('Second Window')
        self.root.geometry("1350x700+0+0")
        self.root.config(bg='white')
        frame1 = Frame(self.root, bg='black')
        frame1.place(x=400, y=50, width=400, height=600)
        btn_1 = Button(frame1, command=self.first_window, text='open first window',
                       font=("Times New Roman", 15, 'bold'), bd=3,
                       relief=RIDGE,
                       cursor='hand2', bg='red', fg='white', activeforeground='white', activebackground='red')
        btn_1.place(x=100, y=350, width=220, height=35)

    def first_window(self):
        self.new_window = Toplevel(self.root)
        self.app = first(self.new_window)


if __name__ == '__main__':
    main()

我知道这个问题很常见,但我在这里找不到适用于我的代码的解决方案。

python user-interface tkinter python-requests tkinter-menu
2个回答
3
投票

您可以使用 Tk 类销毁以前的窗口并启动一个新窗口 这是一个例子

from tkinter import *
from tkinter.ttk import Button
root = Tk()
root.title("title")
root.geometry("800x500")

def window2():
    root.destroy()
    window2_main = Tk()
    Label(window2_main, text="Bye Bye").pack()
    window2_main.mainloop()
a = Button(text="Click This", command=window2)
a.pack()
root.mainloop()


0
投票

您可以尝试这个版本:

import tkinter as tk
from tkinter import ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("400x200")
        self.title("window 1")

        # widget
        self.widgets()

    def widgets(self):
        button = ttk.Button(self, text="Press Me", command=self.open_window)
        button.pack(expand=True)

    def open_window(self):
        self.withdraw() 
        top = TopWindow(self)
        top.protocol("WM_DELETE_WINDOW", self.on_top_window_close)
        top.deiconify() 

    def on_top_window_close(self):
        self.deiconify() 
        self.destroy()


class TopWindow(tk.Toplevel):
    def __init__(self, master):
        super().__init__(master)
        self.title("Window 2")
        self.geometry("600x300")


if __name__ == "__main__":
    main = App()
    main.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.