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

Python Tkinter新窗口打开时自动关闭原窗口的实现问题求助

问题描述

我正在做一个工作项目,上学时学过编程,但现在还是编程新手。我把不同窗口封装在函数里,点击“next step”按钮会触发对应函数打开新窗口,但没法实现新窗口打开时自动关闭原窗口。比如点击“91ERP00500CAA”按钮后,caa1函数对应的新窗口能成功打开,但原窗口没关闭。我试过用root.destroy()方法,但没用,相关代码如下:

class Window:
    def __init__(self, master):
        self.master = root
        root.title("Housing specification")
        master.geometry("1900x1080") #set the size of the screen
        
        #Frame
        self.frame = tk.Frame(self.master, width = 200, height = 200)
        self.frame.pack()
        
        #Label , welcome to spec
        self.label = tk.Label(self.frame, text = "Housing specification")
        self.label.place(x = 10, y = 10)
        
        self.buttoncaa = tk.Button(self.frame, text = "91ERP00500CAA", command = self.caa1)
        self.buttoncaa.place(x = 30, y = 80)
        
        def quit_win():
            win.quit()
   
        
        
       
    #Reference test for CAA specification
    def caa1(self):
        top = Toplevel()
        top.title("91ERP00500CAA - REFERENCE TEST")
        top.geometry("1900x1080")
        reference = PhotoImage(file = r"C:\Users\rnorris\Desktop\3.10.PNG")
        ref = Label(top, image = reference)
        ref.pack()
        
        
        
        self.ns1 = tk.Button(top, text="Next step", command = self.caa2)
        self.ns1.pack()
        
        self.permit1 = tk.Button(top, text="Permits", command = self.permitref)
        self.permit1.pack()
        mainloop()

解决方案

问题出在主窗口引用错误、重复调用主循环,以及缺少正确的销毁逻辑,调整后的代码如下:

import tkinter as tk
from tkinter import Toplevel, Label, PhotoImage

class Window:
    def __init__(self, master):
        self.master = master  # 正确绑定传入的主窗口实例
        self.master.title("Housing specification")
        self.master.geometry("1900x1080")
        
        # Frame
        self.frame = tk.Frame(self.master, width=200, height=200)
        self.frame.pack()
        
        # Label
        self.label = tk.Label(self.frame, text="Housing specification")
        self.label.place(x=10, y=10)
        
        self.buttoncaa = tk.Button(self.frame, text="91ERP00500CAA", command=self.caa1)
        self.buttoncaa.place(x=30, y=80)

    def caa1(self):
        top = Toplevel()
        top.title("91ERP00500CAA - REFERENCE TEST")
        top.geometry("1900x1080")
        
        # 保存图片引用,避免被Python垃圾回收机制清理
        reference = PhotoImage(file=r"C:\Users\rnorris\Desktop\3.10.PNG")
        top.reference = reference
        ref = Label(top, image=reference)
        ref.pack()
        
        self.ns1 = tk.Button(top, text="Next step", command=self.caa2)
        self.ns1.pack()
        
        self.permit1 = tk.Button(top, text="Permits", command=self.permitref)
        self.permit1.pack()
        
        # 关闭原主窗口
        self.master.destroy()

    # 根据需求补充后续窗口逻辑
    def caa2(self):
        pass
    
    def permitref(self):
        pass

if __name__ == "__main__":
    root = tk.Tk()
    app = Window(root)
    root.mainloop()  # 仅在主程序入口调用一次主循环

关键调整点

  • 把self.master = root改为self.master = master,确保类内部正确引用主窗口实例,才能通过self.master.destroy()正常关闭原窗口。
  • 删除caa1中的mainloop(),主循环只需要在程序入口调用一次,重复调用会导致异常。
  • 给PhotoImage对象添加持续引用(top.reference = reference),防止图片因垃圾回收消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 17:35:05