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

Eclipse运行Python Tkinter计算器代码出现缩进错误,求修复方案

计算器代码缩进错误修复方案

错误根因

你的代码存在两处核心缩进问题,直接触发语法报错:

  • 标注报错的_1 = Button(...)行及后续直到_0['font'] = myFont的按钮定义代码,比main函数内的同层级代码多缩进了1层,不符合Python“同层级代码缩进一致”的规则
  • 末尾if __name__ == '__main__':下的main()调用未缩进,也会触发缩进语法错误

同时代码还存在3处不符合作业要求的遗漏点:

  • 输入输出框未设置只读属性
  • 缺少作业要求的小数点按钮
  • 清除按钮显示文本未改为要求的C,错误提示可简化为要求的Error

修复后完整代码

from tkinter import *
from tkinter import font as tkFont # for convenience

expression = ""
# 数字/运算符输入函数
def input_number(number, equation):
    global expression
    # 拼接输入字符串
    expression = expression + str(number)
    equation.set(expression)

# 清空输入框函数
def clear_input_field(equation):
    global expression
    expression = ""
    equation.set("Enter the expression")

# 计算结果函数
def evaluate(equation):
    global expression
    # 尝试计算表达式
    try:
        result = str(eval(expression))
        equation.set(result)
        expression = ""
    except:
        equation.set("Error")
        expression = ""

def main():
    # 创建主窗口
    tk = Tk()
    # 设置窗口标题
    tk.title("Calculator")
    # 设置窗口大小
    tk.geometry("570x580")
    # 实例化字符串变量绑定输入框
    equation = StringVar()
    # 创建只读输入框
    input_field = Entry(tk, textvariable=equation, state='readonly')
    input_field.grid(columnspan=4, ipadx=200, ipady=30)
    equation.set("Enter the expression")

    myFont = tkFont.Font(family='Helvetica', size=12, weight='bold')
    input_field['font']=myFont

    # 创建按钮并放置到对应位置
    _1 = Button(tk, text='1', fg='black', bg='red', bd=2, command=lambda: input_number(1, equation), height=5, width=10)
    _1['font'] = myFont
    _1.grid(row=2, column=0)
    _2 = Button(tk, text='2', fg='black', bg='red', bd=2, command=lambda: input_number(2, equation), height=5, width=10)
    _2.grid(row=2, column=1)
    _3 = Button(tk, text='3', fg='black', bg='red', bd=2, command=lambda: input_number(3, equation), height=5, width=10)
    _3.grid(row=2, column=2)
    _4 = Button(tk, text='4', fg='black', bg='red', bd=2, command=lambda: input_number(4, equation), height=5, width=10)
    _4.grid(row=3, column=0)
    _5 = Button(tk, text='5', fg='black', bg='red', bd=2, command=lambda: input_number(5, equation), height=5, width=10)
    _5.grid(row=3, column=1)
    _6 = Button(tk, text='6', fg='black', bg='red', bd=2, command=lambda: input_number(6, equation), height=5, width=10)
    _6.grid(row=3, column=2)
    _7 = Button(tk, text='7', fg='black', bg='red', bd=2, command=lambda: input_number(7, equation), height=5, width=10)
    _7.grid(row=4, column=0)
    _8 = Button(tk, text='8', fg='black', bg='red', bd=2, command=lambda: input_number(8, equation), height=5, width=10)
    _8.grid(row=4, column=1)
    _9 = Button(tk, text='9', fg='black', bg='red', bd=2, command=lambda: input_number(9, equation), height=5, width=10)
    _9.grid(row=4, column=2)
    _0 = Button(tk, text='0', fg='black', bg='red', bd=2, command=lambda: input_number(0, equation), height=5, width=10)
    _0.grid(row=5, column=0)
    # 新增小数点按钮
    dot = Button(tk, text='.', fg='black', bg='red', bd=2, command=lambda: input_number('.', equation), height=5, width=10)
    dot.grid(row=6, column=0)
    plus = Button(tk, text='+', fg='black', bg='red', bd=2, command=lambda: input_number('+', equation), height=5, width=10)
    plus.grid(row=2, column=3)
    minus = Button(tk, text='-', fg='black', bg='red', bd=2, command=lambda: input_number('-', equation), height=5, width=10)
    minus.grid(row=3, column=3)
    multiply = Button(tk, text='*', fg='black', bg='red', bd=2, command=lambda: input_number('*', equation), height=5, width=10)
    multiply.grid(row=4, column=3)
    divide = Button(tk, text='/', fg='black', bg='red', bd=2, command=lambda: input_number('/', equation), height=5, width=10)
    divide.grid(row=5, column=3)
    equal = Button(tk, text='=', fg='black', bg='red', bd=2, command=lambda: evaluate(equation), height=5, width=10)
    equal.grid(row=5, column=2)
    _1['font'] = myFont
    _2['font'] = myFont
    _3['font'] = myFont
    _4['font'] = myFont
    _5['font'] = myFont
    _6['font'] = myFont
    _7['font'] = myFont
    _8['font'] = myFont
    _9['font'] = myFont
    _0['font'] = myFont
    dot['font'] = myFont
    plus['font'] = myFont
    minus['font'] = myFont
    multiply['font'] = myFont
    divide['font'] = myFont
    equal['font'] = myFont
    clear = Button(tk, text='C', fg='black', bg='red', bd=2, command=lambda: clear_input_field(equation), height=5, width=10)
    clear.grid(row=5, column=1)
    clear['font']=myFont
    # 启动GUI
    tk.mainloop()

if __name__ == '__main__':
    main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 23:45:02