from tkinter import *
from math import *
def btnClick(numbers):
global operator
operator=operator + str(numbers)
text_input.set(operator)
def btnClearDisplay():
global operator
operator=""
text_input.set=("")
def btnEqualsInput():
global operator
sumup=str(eval(operator))
text_input.set(operator)
operator=""
cal = Tk()
cal.title("Calculator")
operator = ""
text_input = StringVar()
txtDisplay = Entry(cal,font=("Helvetica", 15, 'italic'),
textvariable=text_input, bd=18, insertwidth=4,
bg="grey", justify='right') .grid(columnspan=4)
btn7=Button(cal,padx=12,bd=8, fg="black", font=('arial',15,'bold'),
text='7',command=lambda:btnClick(7), bg="silver")
.grid(row=1,column=0)
btn8=Button(cal,padx=12,bd=8, fg="black", font=('arial',15,'bold'),
text='8',command=lambda:btnClick(8), bg="silver")
.grid(row=1,column=1)
btn9=Button(cal,padx=12,bd=8, fg="black", font=('arial',15,'bold'),
text='9',command=lambda:btnClick(9), bg="silver")
.grid(row=1,column=2)
addition=Button(cal,padx=12,bd=8, fg="black", font=('arial',15,'bold'),
text='+', bg="maroon",command=lambda:btnClick("+"))
.grid(row=1,column=3)
btn4=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='4',command=lambda:btnClick(4), bg="lightblue")
.grid(row=2,column=0)
btn5=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='5',command=lambda:btnClick(5), bg="lightblue")
.grid(row=2,column=1)
btn6=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='6',command=lambda:btnClick(6), bg="lightblue")
.grid(row=2,column=2)
subraction=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='-',command=lambda:btnClick("-"), bg="lightblue")
.grid(row=2,column=3)
btn1=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='1',command=lambda:btnClick(1), bg="lightblue")
.grid(row=3,column=0)
btn2=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='2',command=lambda:btnClick(2), bg="lightblue")
.grid(row=3,column=1)
btn3=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='3',command=lambda:btnClick(3), bg="lightblue")
.grid(row=3,column=2)
multiplication=Button(cal, padx=12,bd=8,fg="black",font=("arial", 15, 'bold'),
text='*',command=lambda:btnClick("*"), bg="lightblue") .grid(row=3,column=3)
btn0=Button(cal, padx=12,pady=12,bd=8, fg="black",font=("arial", 15, 'bold'),
text='0',command=lambda:btnClick(0), bg="lightblue") .grid(row=4,column=0)
btnClear=Button(cal, padx=12,pady=12,bd=8, fg="black",font=("arial", 15, 'bold'),
text='C', bg="lightblue", command=btnClearDisplay) .grid(row=4,column=1)
equals=Button(cal, padx=12,pady=12,bd=8, fg="black",font=("arial", 15, 'bold'),
text='=', bg="lightblue", command=btnEqualsInput) .grid(row=4,column=2)
divide=Button(cal, padx=12,pady=12,bd=8, fg="black",font=("arial", 15, 'bold'),
text='/', bg="lightblue", command=lambda:btnClick("/")) .grid(row=4,column=3)
cal.mainloop()
答案 0 :(得分:2)
您的错误是您没有正确调用set
方法。考虑以下代码:
text_input.set=("")
请注意=
,这意味着您要销毁set
方法并将其替换为空字符串。相反,您需要像这样调用set
方法:
text_input.set("")
答案 1 :(得分:0)
还有=按钮。
您的代码:
def btnEqualsInput():
global operator
sumup=str(eval(operator))
text_input.set(operator)
operator=""
问题修复,需要双击,我不知道为什么。
def btnEqualsInput():
global operator
sumup=str(eval(operator))
text_input.set(operator)
operator=sumup
完全固定:
def btnEqualsInput():
global operator
sumup=str(eval(operator))
operator=sumup
text_input.set(operator)