python的tkinter编写计算器
时间: 2023-05-03 12:01:30 浏览: 97
python使用tkinter实现简单计算器
这个问题的意思是:如何用Python的tkinter编写一个计算器。
回答:编写一个计算器可以分为以下几个步骤:
1. 导入tkinter模块,创建一个窗口
2. 在窗口上添加显示计算结果的文本框
3. 添加数字和操作符等按钮,并定义它们的功能
4. 给按钮添加事件响应,让它们能够实现计算功能并更新文本框中的结果
具体实现可以参考以下代码:
```
import tkinter as tk
def calc(num1, num2, operator):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
return num1 / num2
def updateResult(resultVar, num):
if resultVar.get() == '0':
resultVar.set(num)
else:
resultVar.set(resultVar.get() + num)
def clearResult(resultVar):
resultVar.set('0')
def calculate(resultVar):
try:
result = calc(int(num1Var.get()), int(num2Var.get()), operatorVar.get())
resultVar.set(str(result))
except ValueError:
resultVar.set('Error')
def addOperator(operatorVar, operator):
operatorVar.set(operator)
root = tk.Tk()
root.title('Calculator')
num1Var = tk.StringVar(value='0')
num2Var = tk.StringVar(value='0')
operatorVar = tk.StringVar(value='+')
resultVar = tk.StringVar(value='0')
titleLabel = tk.Label(root, text='Calculator', font=('Arial', 16))
titleLabel.grid(row=0, column=0, columnspan=4)
resultLabel = tk.Label(root, textvariable=resultVar, font=('Arial', 16), width=15)
resultLabel.grid(row=1, column=0, columnspan=4)
num1Entry = tk.Entry(root, textvariable=num1Var, font=('Arial', 16), width=5)
num1Entry.grid(row=2, column=0)
operatorLabel = tk.Label(root, textvariable=operatorVar, font=('Arial', 16))
operatorLabel.grid(row=2, column=1)
num2Entry = tk.Entry(root, textvariable=num2Var, font=('Arial', 16), width=5)
num2Entry.grid(row=2, column=2)
equalLabel = tk.Label(root, text='=', font=('Arial', 16))
equalLabel.grid(row=2, column=3)
button1 = tk.Button(root, text='1', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '1'))
button1.grid(row=3, column=0)
button2 = tk.Button(root, text='2', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '2'))
button2.grid(row=3, column=1)
button3 = tk.Button(root, text='3', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '3'))
button3.grid(row=3, column=2)
button4 = tk.Button(root, text='4', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '4'))
button4.grid(row=4, column=0)
button5 = tk.Button(root, text='5', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '5'))
button5.grid(row=4, column=1)
button6 = tk.Button(root, text='6', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '6'))
button6.grid(row=4, column=2)
button7 = tk.Button(root, text='7', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '7'))
button7.grid(row=5, column=0)
button8 = tk.Button(root, text='8', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '8'))
button8.grid(row=5, column=1)
button9 = tk.Button(root, text='9', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '9'))
button9.grid(row=5, column=2)
button0 = tk.Button(root, text='0', font=('Arial', 16), width=5, command=lambda:updateResult(resultVar, '0'))
button0.grid(row=6, column=1)
buttonPlus = tk.Button(root, text='+', font=('Arial', 16), width=5, command=lambda:addOperator(operatorVar, '+'))
buttonPlus.grid(row=3, column=3)
buttonMinus = tk.Button(root, text='-', font=('Arial', 16), width=5, command=lambda:addOperator(operatorVar, '-'))
buttonMinus.grid(row=4, column=3)
buttonMultiply = tk.Button(root, text='*', font=('Arial', 16), width=5, command=lambda:addOperator(operatorVar, '*'))
buttonMultiply.grid(row=5, column=3)
buttonDivide = tk.Button(root, text='/', font=('Arial', 16), width=5, command=lambda:addOperator(operatorVar, '/'))
buttonDivide.grid(row=6, column=3)
buttonClear = tk.Button(root, text='Clear', font=('Arial', 16), width=5, command=lambda:clearResult(resultVar))
buttonClear.grid(row=6, column=0)
buttonCalculate = tk.Button(root, text='Calculate', font=('Arial', 16), width=10, command=lambda:calculate(resultVar))
buttonCalculate.grid(row=7, column=0, columnspan=4)
root.mainloop()
```
这段代码能够创建一个基本的计算器界面,并能够实现基本的加、减、乘、除计算。其中,lambda表达式用于将函数参数传递给事件响应函数,事件响应函数通过调用updateResult()函数来更新文本框中的数字,调用addOperator()函数来更新操作符,调用clearResult()函数来清除文本框中的数字。calculate()函数用于计算结果,并在文本框中显示结果。
阅读全文