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

AttributeError:'finance_tracker'对象无'back_button'属性求助

问题分析

报错AttributeError: 'finance_tracker' object has no attribute 'back_button'的核心原因是执行顺序错误:
在display_transactions函数中,你先调用了self.hide_buttons(),但此时self.back_button、self.remove_all、self.remove_one这些属性还未被创建——这些按钮是在后续的self.create_widgets(view_transaction_frame)中才初始化的。提前调用hide_buttons会导致程序试图访问不存在的对象属性,触发错误。

另外注意到create_widgets函数的最后一行已经调用了self.hide_buttons(),所以display_transactions中的self.hide_buttons()属于重复且错误的调用。

解决方案

直接删除display_transactions函数中提前调用的self.hide_buttons()即可,修改后的display_transactions代码如下:

def display_transactions(self):
    view_transaction_frame = tk.Toplevel()
    view_transaction_frame.title('Display Transactions')
    view_transaction_frame.configure(bg="Light Gray")
    view_transaction_frame.geometry('700x400')
    view_transaction_frame.resizable(False, False)     
    view_transaction_frame.iconbitmap(r'C:\Users\USER\Desktop\SD1_Courseworks\COURSEWORK_03\tracker_icon.ico')

    # create labels
    display_title = tk.Label(view_transaction_frame, text="The Recorded Transactions are as follows",
                             anchor="center", font=TITLE_FONT_STYLE, bg='Light Gray')
    display_title.pack(pady=20)

    self.create_widgets(view_transaction_frame)
额外优化建议

如果后续需要灵活控制按钮显示/隐藏,建议在类的__init__方法中先初始化这些按钮属性为None,避免属性未定义的风险:

def __init__(self, ...):
    # 其他初始化代码
    self.back_button = None
    self.remove_all = None
    self.remove_one = None

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 10:05:06