这是我的代码:
from Tkinter import*
calculator = Tk()
calculator.title('Calcualtor')
calculator.geometry('300x325')
screen = Frame(calculator, bd=2, width=250, height=25, relief=SUNKEN)
buttons = Frame(calculator, bd=2, width=250, height=200)
screen.grid(column=0, row=0, padx=25, pady=25)
buttons.grid(column=0, row=1, padx=25)
def appear():
results.insert(0, "0")
return
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for index in range(9):
Button(buttons, bg="White", text=numbers[index], width=5, height=2, command=appear).grid(padx=5, pady=5, row=index%3, column=index/3)
zero= Button(buttons, bg="White", text="0", width=5, height=2)
zero.grid(padx=5, pady=5, column=1, row=3)
functions=["-", "+", "*", "/"]
for index in range(4):
Button(buttons, bg="White", text=functions[index], width=5, height=2).grid(padx=5, pady=5, row=index%4, column=3)
equals= Button(buttons, bg="White", text="=", width=5, height=2)
equals.grid(ipadx=10, pady=5, row=5, column=1)
numbers = StringVar()
results = Entry(screen, textvariable=numbers, width=30)
results.pack()
calculator.mainloop()
计算器看起来不错,但我需要帮助让它在按下按钮时显示一个数字。正如您在我的函数中看到的那样,现在每次按下按钮时它都会显示“0”,而不是相应的数字。请帮忙。我还没有开始弄清楚如何让它做实际的数学,但如果你也能帮忙,那就太好了!