You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter页面切换:如何关闭页面时调用另一个类实现循环切换?

Tkinter 双向页面切换问题

问题描述

需要实现Tkinter页面的双向切换:关闭第一个页面后打开第二个页面,完成第二个页面的提交操作后返回第一个页面。每个页面使用独立类实现,目前仅能从第一个页面跳转到第二个页面,无法反向返回。

问题根源

  1. 原代码中,WelcomePage跳转时直接销毁了自身窗口self.window,导致后续NewAccountPage无法复用已销毁的窗口返回。
  2. 错误尝试调用已销毁窗口的mainloop()方法,这在Tkinter中完全无效。

修正后的代码

必要导入模块

import tkinter as tk
from PIL import Image, ImageTk

欢迎页面(WelcomePage)

class WelcomePage():
    # 第一个页面
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Welcome!")
        self.window.geometry("300x300")
        self.window.configure(bg="light green")

        self.welcome_frame = tk.LabelFrame(self.window,
                                           background="light blue",
                                           foreground="light blue")
        self.welcome_frame.pack(pady=15)

        self.welcome_label = tk.Label(self.welcome_frame, text="WELCOME!",
                                      background="light grey",
                                      foreground="blue")
        self.welcome_label.pack()

        self.account_dropbox_frame = tk.LabelFrame(self.window)
        self.account_dropbox_frame.configure(bg="light blue",
                                             fg="light blue")
        
        self.account_dropbox_frame.pack(pady=15)

        self.account_dropbox_var = tk.StringVar(self.window)
        self.account_dropbox_var.set("one")
        self.account_dropbox = tk.OptionMenu(self.account_dropbox_frame, self.account_dropbox_var,
                                             "one", "two", "three")
        
        self.account_dropbox.configure(bg="light blue",
                                       fg="blue",
                                       highlightbackground="light blue")
        self.account_dropbox.pack()

        self.group_dropbox_frame = tk.LabelFrame(self.window)
        self.group_dropbox_frame.configure(bg="light blue",
                                           fg="light blue")
        
        self.group_dropbox_frame.pack(pady=15)

        self.group_dropbox_var = tk.StringVar()
        self.group_dropbox_var.set("one")
        self.group_dropbox = tk.OptionMenu(self.group_dropbox_frame,
                                           self.group_dropbox_var,
                                           "one", "two", "three")
        
        self.group_dropbox.configure(bg="light blue",
                                     fg="blue",
                                     highlightbackground="light blue")
        self.group_dropbox.pack()

        self.button_frame = tk.LabelFrame(self.window)
        self.button_frame.configure(bg="light blue")
        self.button_frame.pack()

        self.button_frame.columnconfigure(0, weight=1)
        self.button_frame.columnconfigure(1, weight=1)
        self.button_frame.rowconfigure(0, weight=1)

        self.new_account_button = tk.Button(self.button_frame,
                                            text="New Account",
                                            command=self.start_account_page,
                                            foreground="blue",
                                            background="light blue")
        
        self.new_group_button = tk.Button(self.button_frame,
                                          text="New Group",
                                          foreground="blue",
                                          background="light blue")

        self.new_account_button.grid(row=0, column=0)
        self.new_group_button.grid(row=0, column=1)

        # 注意:替换为你本地的图片路径
        self.load = Image.open(r"C:\Users\pc\Desktop\Telegram_bot\right_arrow.jpg").convert("RGBA")
        self.resized_img = self.load.resize((25,25))
        
        self.render = ImageTk.PhotoImage(self.resized_img)
        self.click_btn = tk.Button(self.window, image=self.render)
        self.click_btn.place(relx= 0.90, rely=0.90)

    def start_account_page(self):
        # 销毁当前窗口,打开新账户页面
        self.window.destroy()
        new_account_page = NewAccountPage()
        new_account_page.run()
    
    def run(self):
        self.window.mainloop()

新账户页面(NewAccountPage)

class NewAccountPage():
    # 第二个页面
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("New Account")
        self.window.geometry("300x300")

        self.window.rowconfigure(0, weight=1)
        self.window.rowconfigure(1, weight=1)
        self.window.rowconfigure(2, weight=1)
        self.window.rowconfigure(3, weight=1)
        self.window.columnconfigure(0, weight=1)

        self.new_account_label = tk.Label(self.window, text="NEW ACCOUNT", foreground="red")
        self.new_account_label.grid(row=0, column=0)

        self.entry_frame = tk.LabelFrame(self.window)
        self.entry_frame.grid(row=1, column=0)

        self.api_hash_label = tk.Label(self.entry_frame, text="Enter API Hash")
        self.api_hash_label.pack()

        self.api_hash_entry = tk.Entry(self.entry_frame)
        self.api_hash_entry.pack()

        self.api_id_label = tk.Label(self.entry_frame, text="Enter API ID")
        self.api_id_label.pack()

        self.api_id_entry = tk.Entry(self.entry_frame)
        self.api_id_entry.pack()

        self.phone_label = tk.Label(self.entry_frame, text="Enter Phone Number")
        self.phone_label.pack()

        self.phone_entry = tk.Entry(self.entry_frame)
        self.phone_entry.pack()

        self.submit_button = tk.Button(self.window, text="Submit", command=self.submit_account)
        self.submit_button.grid(row=2, column=0)

    def submit_account(self):
        # 销毁当前窗口,返回欢迎页面
        self.window.destroy()
        welcome_page = WelcomePage()
        welcome_page.run()

    def run(self):
        self.window.mainloop()

程序运行入口

if __name__ == "__main__":
    app = WelcomePage()
    app.run()

内容的提问来源于stack exchange,提问作者BajranGun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:22:04