【发布时间】:2022-01-06 18:46:50
【问题描述】:
我的 Eclipse 编译器出现缩进错误,我已在下面的特定行上标记了错误所在的位置。有人对修复有任何想法吗?这是学校的一项作业,我不知道如何解决,我完全完全不知所措。我会很感激任何人的提示。 “编写一个基于 GUI 的程序来模拟一个简单的袖珍计算器。GUI 显示一个用于输出的输入字段(我建议将其设为只读字段)。它应该为用户提供从 0 到 9 和 7 的 10 个数字按钮+、-、*、/、C(C 用于清除显示)、.(小数点)和 =.= 的功能键用于计算正确答案。如果出现除以零之类的错误,则输入字段应显示 ERR 或错误。输入字段中的值将构建一个字符串,该字符串将转换为浮点数进行计算。”这是我必须完成的任务。
from tkinter import *
from tkinter import font as tkFont # for convenience
expression = ""
# functions
def input_number(number, equation):
global expression
# concatenation of string
expression = expression + str(number)
equation.set(expression)
def clear_input_field(equation):
global expression
expression = ""
# setting empty string in the input field
equation.set("Enter the expression")
def evaluate(equation):
global expression
# trying to evaluate the expression
try:
result = str(eval(expression))
equation.set(result)
expression = ""
except:
equation.set("Error: Divide by zero")
expression = ""
def main():
# creating main window
tk = Tk()
# setting the title
tk.title("Calculator")
# seting the size of window
tk.geometry("570x500")
# varible class instantiation
equation = StringVar()
# input field for the expression
input_field = Entry(tk, textvariable=equation)
input_field.place(height=100)
input_field.grid(columnspan=4, ipadx=200, ipady=10)
equation.set("Enter the expression")
myFont = tkFont.Font(family='Helvetica', size=12, weight='bold')
input_field['font']=myFont
# creating buttons and placing them at respective positions
_1 = Button(tk, text='1', fg='black', bg='red', bd=2, command=lambda: input_number(1, equation), height=5, width=10) #I'm getting an error on this line
_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)
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
plus['font'] = myFont
minus['font'] = myFont
multiply['font'] = myFont
divide['font'] = myFont
equal['font'] = myFont
clear = Button(tk, text='Clear', 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
# showing the GUI continue
tk.mainloop()
if __name__ == '__main__':
main()```
【问题讨论】:
-
很明显很多行没有排成一行。
-
附带说明,您应该将按钮存储在一个数组中,而不是拥有 10 个单独的变量。然后你可以做
for b in btns:/b['font'] = myFont。 -
# creating buttons and placing them at respective positions之后直到plus['font'] = myFont的行有一些冗余空格(2 个空格)。此外,main()前面必须有(4 个空格)。这些在您的代码中没有观察到,这与预期错误有关。
标签: python tkinter computer-science