python 简易计算器
时间: 2023-12-02 20:43:01 浏览: 70
Python简易计算器
5星 · 资源好评率100%
以下是一个使用Python的tkinter库实现的简易计算器界面设计的例子:
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
# 创建显示框
self.display = tk.Entry(master, width=30, justify='right', font=('Arial', 16))
self.display.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 创建按钮
self.create_button('7', 1, 0)
self.create_button('8', 1, 1)
self.create_button('9', 1, 2)
self.create_button('/', 1, 3)
self.create_button('4', 2, 0)
self.create_button('5', 2, 1)
self.create_button('6', 2, 2)
self.create_button('*', 2, 3)
self.create_button('1', 3, 0)
self.create_button('2', 3, 1)
self.create_button('3', 3, 2)
self.create_button('-', 3, 3)
self.create_button('0', 4, 0)
self.create_button('.', 4, 1)
self.create_button('C', 4, 2)
self.create_button('+', 4, 3)
self.create_button('=', 5, 0, columnspan=4)
def create_button(self, text, row, column, rowspan=1, columnspan=1):
button = tk.Button(self.master, text=text, width=5, height=2, font=('Arial', 16),
command=lambda: self.button_click(text))
button.grid(row=row, column=column, rowspan=rowspan, columnspan=columnspan, padx=5, pady=5)
def button_click(self, text):
if text == '=':
try:
result = eval(self.display.get())
self.display.delete(0, tk.END)
self.display.insert(0, result)
except:
self.display.delete(0, tk.END)
self.display.insert(0, 'Error')
elif text == 'C':
self.display.delete(0, tk.END)
else:
self.display.insert(tk.END, text)
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
这个计算器界面包含数字、小数点、加减乘除、清除和等于按钮。用户可以通过点击按钮来输入数字和运算符,并在显示框中看到计算结果。
阅读全文