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

如何修复AttributeError: 'Window'对象无'submit_button'属性错误

解决AttributeError: 'Window' object has no attribute 'submit_button'错误

你的问题核心是代码执行顺序错误:第一次调用creation方法时,开头直接访问self.submit_button['text'],但这个按钮是在方法后半段才创建的——此时self.submit_button还未被定义,必然触发属性不存在的错误。

修复方案

方案一:调整代码顺序,先创建按钮再判断

把submit_button的创建代码移到判断逻辑之前,确保第一次调用时属性已经存在:

def creation(self):
    # 先创建submit_button
    self.submit_button = tk.Button(text="Create a file", command=self.creator, height=3, width=20)
    
    # 再执行文本判断逻辑
    if self.submit_button['text'] != "Create a file":
        time.sleep(3)
        self.submit_button['text'] = "Create a file"
        self.submit_button['foreground'] = "black"

    # 以下是原有的其余控件创建和布局代码
    self.creation_title = tk.Label(text="File creator:", font=("arial", 25))
    self.name_label = tk.Label(text="Name:")
    self.age_label = tk.Label(text="Age:")
    self.height_label = tk.Label(text="Height:")
    self.citizen_status_label = tk.Label(text="Citizenship Status:")
    self.back_button = tk.Button(text="close", command=lambda: [self.options(), self.destroyer("creation")],
                                 height=1, width=5)

    self.name_entry = tk.Entry(self.root)
    self.age_entry = tk.Entry(self.root)
    self.height_entry = tk.Entry(self.root)
    self.citizen_status = tk.StringVar()
    self.citizen_status_valid = tk.Radiobutton(text="Valid", variable=self.citizen_status, value="VALID")
    self.citizen_status_invalid = tk.Radiobutton(text="Invalid", variable=self.citizen_status, value="INVALID")

    self.creation_title.place(x=20, y=25)
    self.name_label.place(x=250, y=97.5)
    self.age_label.place(x=262, y=137.5)
    self.height_label.place(x=248, y=177.5)
    self.citizen_status_label.place(x=191, y=217.5)

    self.submit_button.place(x=20, y=150)
    self.back_button.place(x=20, y=250)

    self.name_entry.place(x=300, y=100)
    self.age_entry.place(x=300, y=140)
    self.height_entry.place(x=300, y=180)
    self.citizen_status_valid.place(x=295, y=220)
    self.citizen_status_invalid.place(x=362.5, y=220)

    print("Creation Menu Created")

方案二:在类初始化时提前创建按钮(适合复用场景)

如果这个按钮需要在多个方法中复用,可在类的__init__方法中提前初始化按钮(先隐藏),后续直接修改属性:

def __init__(self, root):
    self.root = root
    # 提前创建按钮并隐藏
    self.submit_button = tk.Button(root, text="Create a file", command=self.creator, height=3, width=20)
    self.submit_button.place_forget()

def creation(self):
    # 此时按钮已经存在,可直接判断修改
    if self.submit_button['text'] != "Create a file":
        time.sleep(3)
        self.submit_button['text'] = "Create a file"
        self.submit_button['foreground'] = "black"
    
    # 显示按钮
    self.submit_button.place(x=20, y=150)

    # 其余控件创建和布局代码不变...

方案三:添加属性存在性检查(灵活避免重复创建)

如果不确定按钮是否已创建,先判断属性是否存在再执行逻辑:

def creation(self):
    # 仅当按钮已存在且文本不符合时执行修改
    if hasattr(self, 'submit_button') and self.submit_button['text'] != "Create a file":
        time.sleep(3)
        self.submit_button['text'] = "Create a file"
        self.submit_button['foreground'] = "black"
    
    # 若按钮不存在则创建
    if not hasattr(self, 'submit_button'):
        self.submit_button = tk.Button(text="Create a file", command=self.creator, height=3, width=20)

    # 其余控件创建和布局代码不变...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 00:01:03