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

Tkinter Toplevel窗口控件不显示报错及跨类访问控件方案

问题根因

报错和控件不显示的核心原因非常明确:你给子窗口内所有控件指定父容器时,传入的是Window这个类本身,而不是Window实例化后的窗口对象self。
Tkinter要求所有控件初始化时第一个参数必须是已完成实例化的父控件对象,类本身不携带tk运行时实例的相关属性,所以才会抛出type object 'Window' has no attribute 'tk'的异常,控件也因为父容器参数非法无法正常渲染。
另外你代码末尾多写了一个用pack布局的重复Close按钮,会打乱其他用place定位的控件布局,直接删除即可。

修复后可运行代码
import tkinter as tk
from tkinter import ttk
from tkinter import *

class Window(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        self.geometry('1000x600+100+50')
        self.title('Aplication Ksk')

        # 所有控件父容器从Window类改为self(当前Toplevel实例)
        self.list_box2= Listbox(self, width='65', height='18')
        self.list_box3= Listbox(self, width='65', height='18')

        self.io_number=Label(self, text = 'IO number: ')
        self.wo_number=Label(self, text = 'WO: ')
        self.operator_lbl=Label(self, text = 'Operator')
        self.sequence=Label(self, text = 'Sequence')
        self.left_wires_names=Label(self, text = 'Left wires')
        self.right_wires_names=Label(self, text = 'Right wires')

        self.show_sp2=Entry(self)
        self.show_sp1=Entry(self)
        self.show_sequence=Entry(self)
        self.show_io=Entry(self)
        self.show_wo=Entry(self)
        self.operator_=Entry(self)

        self.btn_close = Button(self, text="Close", command=self.destroy)
        self.btn_save = Button(self, text="Save")
        self.btn_print = Button(self, text="print QR code")

        #########################
        self.show_sp2.place(x=80, y=60)
        self.show_sp1.place(x= 300, y=60)
        self.show_sequence.place(x=770, y=60)
        self.sequence.place(x=770, y=35)
        self.btn_print.place(x=770, y=85)

        self.io_number.place(x=80, y=140)
        self.show_io.place(x=150, y=140)
        self.wo_number.place(x=300, y=140)
        self.show_wo.place(x=335, y=140)
        self.operator_lbl.place(x=510, y=140)
        self.operator_.place(x=570, y=140)

        self.list_box2.place(x=80, y= 220)
        self.list_box3.place(x=510, y=220)

        self.left_wires_names.place(x=257, y=195)
        self.right_wires_names.place(x=680, y=195)

        self.btn_close.place(x=257, y=530)
        self.btn_save.place(x=680, y=530)


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('300x200')
        self.title('Main Window')

        ttk.Button(self,
                text='Open a window',
                command=self.open_window).pack(expand=True)

    def open_window(self):
        # 把子窗口存为实例属性,方便后续跨类访问
        self.sub_window = Window(self)
        self.sub_window.grab_set()
        # 示例:跨类访问子窗口控件,给IO输入框插入默认值
        self.sub_window.show_io.insert(0, "TEST-IO-001")


if __name__ == "__main__":
    app = App()
    app.mainloop()
大型Tkinter应用跨类访问控件的常用方案
  • 实例属性直接引用:这是最常用也最易维护的方式。你现在把控件绑定到self的写法本身就是符合规范的,只要持有对应窗口类的实例,就能直接通过实例.控件名操作控件。比如上面修复代码里,App类打开子窗口时把Window实例存为self.sub_window,之后在App类的任意方法里都可以直接读写self.sub_window.show_io、self.sub_window.list_box2等控件。如果子窗口需要访问主窗口控件,只要在子窗口初始化时把父类实例存为self.parent = parent,就能通过self.parent.xxx访问主窗口的控件。
  • 控件name定位:给需要跨层级访问的控件设置全局唯一的name参数,比如Entry(self, name="io_input_entry"),之后只要拿到根窗口(Tk实例)的引用,就可以通过root.nametowidget("io_input_entry")直接获取控件对象,不用层层传递窗口实例,适合控件嵌套层级很深的场景。
  • 控制器解耦:项目规模大到一定程度时,不要让各个UI类之间互相直接持有引用,单独写一个控制器类统一管理公共状态、控件引用和业务逻辑,所有UI类只持有控制器的引用,控件读写、事件触发都通过控制器中转,避免代码耦合过高难以维护。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 05:06:29