python 写一个计算器界面
时间: 2023-10-16 08:09:20 浏览: 68
可以使用 Python 自带的 Tkinter 模块来实现一个简单的计算器界面。以下是一个基本的示例代码:
```python
from tkinter import *
# 定义计算器界面
class Calculator:
def __init__(self, master):
self.master = master
master.title("计算器")
# 创建输入框
self.entry = Entry(master, width=35, borderwidth=5)
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 创建数字按钮
self.button_1 = Button(master, text="1", padx=40, pady=20, command=lambda: self.button_click(1))
self.button_2 = Button(master, text="2", padx=40, pady=20, command=lambda: self.button_click(2))
self.button_3 = Button(master, text="3", padx=40, pady=20, command=lambda: self.button_click(3))
self.button_4 = Button(master, text="4", padx=40, pady=20, command=lambda: self.button_click(4))
self.button_5 = Button(master, text="5", padx=40, pady=20, command=lambda: self.button_click(5))
self.button_6 = Button(master, text="6", padx=40, pady=20, command=lambda: self.button_click(6))
self.button_7 = Button(master, text="7", padx=40, pady=20, command=lambda: self.button_click(7))
self.button_8 = Button(master, text="8", padx=40, pady=20, command=lambda: self.button_click(8))
self.button_9 = Button(master, text="9", padx=40, pady=20, command=lambda: self.button_click(9))
self.button_0 = Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
# 创建操作按钮
self.button_add = Button(master, text="+", padx=39, pady=20, command=self.button_add)
self.button_subtract = Button(master, text="-", padx=41, pady=20, command=self.button_subtract)
self.button_multiply = Button(master, text="*", padx=40, pady=20, command=self.button_multiply)
self.button_divide = Button(master, text="/", padx=41, pady=20, command=self.button_divide)
self.button_equal = Button(master, text="=", padx=91, pady=20, command=self.button_equal)
self.button_clear = Button(master, text="清除", padx=79, pady=20, command=self.button_clear)
# 将按钮添加到界面
self.button_1.grid(row=3, column=0)
self.button_2.grid(row=3, column=1)
self.button_3.grid(row=3, column=2)
self.button_4.grid(row=2, column=0)
self.button_5.grid(row=2, column=1)
self.button_6.grid(row=2, column=2)
self.button_7.grid(row=1, column=0)
self.button_8.grid(row=1, column=1)
self.button_9.grid(row=1, column=2)
self.button_0.grid(row=4, column=0)
self.button_clear.grid(row=4, column=1, columnspan=2)
self.button_add.grid(row=5, column=0)
self.button_equal.grid(row=5, column=1, columnspan=2)
self.button_subtract.grid(row=6, column=0)
self.button_multiply.grid(row=6, column=1)
self.button_divide.grid(row=6, column=2)
self.equation = ""
self.result = ""
# 处理数字按钮点击事件
def button_click(self, number):
current = self.entry.get()
self.entry.delete(0, END)
self.entry.insert(0, str(current) + str(number))
# 处理加法操作
def button_add(self):
first_number = self.entry.get()
self.equation = str(first_number) + "+"
self.entry.delete(0, END)
# 处理减法操作
def button_subtract(self):
first_number = self.entry.get()
self.equation = str(first_number) + "-"
self.entry.delete(0, END)
# 处理乘法操作
def button_multiply(self):
first_number = self.entry.get()
self.equation = str(first_number) + "*"
self.entry.delete(0, END)
# 处理除法操作
def button_divide(self):
first_number = self.entry.get()
self.equation = str(first_number) + "/"
self.entry.delete(0, END)
# 处理等号操作
def button_equal(self):
second_number = self.entry.get()
self.entry.delete(0, END)
if self.equation.endswith("+"):
self.result = float(self.equation[:-1]) + float(second_number)
elif self.equation.endswith("-"):
self.result = float(self.equation[:-1]) - float(second_number)
elif self.equation.endswith("*"):
self.result = float(self.equation[:-1]) * float(second_number)
elif self.equation.endswith("/"):
self.result = float(self.equation[:-1]) / float(second_number)
self.entry.insert(0, self.result)
# 处理清除操作
def button_clear(self):
self.entry.delete(0, END)
self.equation = ""
self.result = ""
# 启动计算器界面
root = Tk()
calculator = Calculator(root)
root.mainloop()
```
运行代码后即可看到计算器界面。注意,此示例只包含基本的加减乘除操作,若需添加其他功能还需进行进一步的开发。
阅读全文