写一个带画面的计算器的Python程序
时间: 2023-10-07 17:10:23 浏览: 53
python代码编写计算器小程序
以下是一个带画面的计算器Python程序,使用了Tkinter模块来创建图形界面:
```
import tkinter as tk
# 定义计算器类
class Calculator:
def __init__(self, master):
self.master = master
master.title("计算器")
# 创建输入框
self.entry = tk.Entry(master, width=40, borderwidth=5)
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 创建数字按钮
self.button_1 = tk.Button(master, text="1", padx=40, pady=20, command=lambda: self.button_click(1))
self.button_2 = tk.Button(master, text="2", padx=40, pady=20, command=lambda: self.button_click(2))
self.button_3 = tk.Button(master, text="3", padx=40, pady=20, command=lambda: self.button_click(3))
self.button_4 = tk.Button(master, text="4", padx=40, pady=20, command=lambda: self.button_click(4))
self.button_5 = tk.Button(master, text="5", padx=40, pady=20, command=lambda: self.button_click(5))
self.button_6 = tk.Button(master, text="6", padx=40, pady=20, command=lambda: self.button_click(6))
self.button_7 = tk.Button(master, text="7", padx=40, pady=20, command=lambda: self.button_click(7))
self.button_8 = tk.Button(master, text="8", padx=40, pady=20, command=lambda: self.button_click(8))
self.button_9 = tk.Button(master, text="9", padx=40, pady=20, command=lambda: self.button_click(9))
self.button_0 = tk.Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
# 创建运算符按钮
self.button_add = tk.Button(master, text="+", padx=39, pady=20, command=self.button_add)
self.button_subtract = tk.Button(master, text="-", padx=41, pady=20, command=self.button_subtract)
self.button_multiply = tk.Button(master, text="*", padx=40, pady=20, command=self.button_multiply)
self.button_divide = tk.Button(master, text="/", padx=41, pady=20, command=self.button_divide)
self.button_equal = tk.Button(master, text="=", padx=91, pady=20, command=self.button_equal)
self.button_clear = tk.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_subtract.grid(row=6, column=0)
self.button_multiply.grid(row=6, column=1)
self.button_divide.grid(row=6, column=2)
self.button_equal.grid(row=5, column=1, columnspan=2)
# 定义变量
self.num1 = None
self.num2 = None
self.operator = None
self.result = None
# 处理数字按钮点击事件
def button_click(self, number):
current = self.entry.get()
self.entry.delete(0, tk.END)
self.entry.insert(0, str(current) + str(number))
# 处理运算符按钮点击事件
def button_add(self):
self.num1 = float(self.entry.get())
self.operator = "+"
self.entry.delete(0, tk.END)
def button_subtract(self):
self.num1 = float(self.entry.get())
self.operator = "-"
self.entry.delete(0, tk.END)
def button_multiply(self):
self.num1 = float(self.entry.get())
self.operator = "*"
self.entry.delete(0, tk.END)
def button_divide(self):
self.num1 = float(self.entry.get())
self.operator = "/"
self.entry.delete(0, tk.END)
# 处理等于号按钮点击事件
def button_equal(self):
self.num2 = float(self.entry.get())
self.entry.delete(0, tk.END)
if self.operator == "+":
self.result = self.num1 + self.num2
elif self.operator == "-":
self.result = self.num1 - self.num2
elif self.operator == "*":
self.result = self.num1 * self.num2
elif self.operator == "/":
self.result = self.num1 / self.num2
self.entry.insert(0, self.result)
# 处理清空按钮点击事件
def button_clear(self):
self.entry.delete(0, tk.END)
self.num1 = None
self.num2 = None
self.operator = None
self.result = None
# 创建主窗口
root = tk.Tk()
# 创建计算器对象
calculator = Calculator(root)
# 运行主循环
root.mainloop()
```
该程序通过Tkinter模块创建了一个图形界面,包括数字按钮、运算符按钮、输入框和清空按钮等,用户可以通过点击按钮来进行数字输入和四则运算。在程序中,通过定义一个Calculator类来处理各个按钮的点击事件,并将结果显示在输入框中。
阅读全文