python 计算器实现,包含标准、科学、程序员,带有界面操作的
时间: 2023-07-10 17:08:51 浏览: 107
Python控制台实现交互式环境执行
可以使用Python内置的Tkinter库来实现一个简单的计算器界面,并在其基础上实现标准、科学、程序员计算器功能。下面是一个简单的示例代码:
```python
import tkinter as tk
import math
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
# 创建文本框
self.display = tk.Entry(master, width=35, borderwidth=5)
self.display.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_equal = tk.Button(master, text="=", padx=91, pady=20, command=self.button_equal)
self.button_clear = tk.Button(master, text="Clear", 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.mode = "standard"
self.create_standard_buttons()
def create_standard_buttons(self):
# 清空按钮
self.button_clear.config(text="Clear", command=self.button_clear)
# 数字按钮
self.button_1.config(command=lambda: self.button_click(1))
self.button_2.config(command=lambda: self.button_click(2))
self.button_3.config(command=lambda: self.button_click(3))
self.button_4.config(command=lambda: self.button_click(4))
self.button_5.config(command=lambda: self.button_click(5))
self.button_6.config(command=lambda: self.button_click(6))
self.button_7.config(command=lambda: self.button_click(7))
self.button_8.config(command=lambda: self.button_click(8))
self.button_9.config(command=lambda: self.button_click(9))
self.button_0.config(command=lambda: self.button_click(0))
# 操作符按钮
self.button_add.config(command=self.button_add)
def create_scientific_buttons(self):
# 清空按钮
self.button_clear.config(text="C", command=self.button_clear)
# 数字按钮
self.button_1.config(command=lambda: self.button_click(1))
self.button_2.config(command=lambda: self.button_click(2))
self.button_3.config(command=lambda: self.button_click(3))
self.button_4.config(command=lambda: self.button_click(4))
self.button_5.config(command=lambda: self.button_click(5))
self.button_6.config(command=lambda: self.button_click(6))
self.button_7.config(command=lambda: self.button_click(7))
self.button_8.config(command=lambda: self.button_click(8))
self.button_9.config(command=lambda: self.button_click(9))
self.button_0.config(command=lambda: self.button_click(0))
self.button_pi = tk.Button(self.master, text="π", padx=39, pady=20, command=lambda: self.button_click(math.pi))
self.button_e = tk.Button(self.master, text="e", padx=40, pady=20, command=lambda: self.button_click(math.e))
self.button_sin = tk.Button(self.master, text="sin", padx=33, pady=20, command=self.button_sin)
self.button_cos = tk.Button(self.master, text="cos", padx=33, pady=20, command=self.button_cos)
self.button_tan = tk.Button(self.master, text="tan", padx=33, pady=20, command=self.button_tan)
self.button_log = tk.Button(self.master, text="log", padx=36, pady=20, command=self.button_log)
# 操作符按钮
self.button_add.config(command=self.button_add)
# 布局按钮
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_pi.grid(row=1, column=3)
self.button_e.grid(row=2, column=3)
self.button_sin.grid(row=4, column=1)
self.button_cos.grid(row=4, column=2)
self.button_tan.grid(row=4, column=3)
self.button_log.grid(row=5, column=0)
def create_programmer_buttons(self):
# 清空按钮
self.button_clear.config(text="C", command=self.button_clear)
# 数字按钮
self.button_1.config(command=lambda: self.button_click(1))
self.button_2.config(command=lambda: self.button_click(2))
self.button_3.config(command=lambda: self.button_click(3))
self.button_4.config(command=lambda: self.button_click(4))
self.button_5.config(command=lambda: self.button_click(5))
self.button_6.config(command=lambda: self.button_click(6))
self.button_7.config(command=lambda: self.button_click(7))
self.button_8.config(command=lambda: self.button_click(8))
self.button_9.config(command=lambda: self.button_click(9))
self.button_0.config(command=lambda: self.button_click(0))
self.button_a = tk.Button(self.master, text="A", padx=39, pady=20, command=lambda: self.button_click("A"))
self.button_b = tk.Button(self.master, text="B", padx=40, pady=20, command=lambda: self.button_click("B"))
self.button_c = tk.Button(self.master, text="C", padx=33, pady=20, command=lambda: self.button_click("C"))
self.button_d = tk.Button(self.master, text="D", padx=36, pady=20, command=lambda: self.button_click("D"))
self.button_e = tk.Button(self.master, text="E", padx=33, pady=20, command=lambda: self.button_click("E"))
self.button_f = tk.Button(self.master, text="F", padx=33, pady=20, command=lambda: self.button_click("F"))
# 操作符按钮
self.button_add.config(command=self.button_add)
# 布局按钮
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_a.grid(row=1, column=3)
self.button_b.grid(row=2, column=3)
self.button_c.grid(row=4, column=1)
self.button_d.grid(row=4, column=2)
self.button_e.grid(row=5, column=1)
self.button_f.grid(row=5, column=2)
def button_click(self, number):
current = self.display.get()
self.display.delete(0, tk.END)
self.display.insert(0, str(current) + str(number))
def button_clear(self):
self.display.delete(0, tk.END)
def button_add(self):
first_number = self.display.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
self.display.delete(0, tk.END)
def button_equal(self):
second_number = self.display.get()
self.display.delete(0, tk.END)
if math == "addition":
self.display.insert(0, f_num + int(second_number))
if self.mode == "scientific":
if math == "sin":
self.display.insert(0, math.sin(float(second_number)))
elif math == "cos":
self.display.insert(0, math.cos(float(second_number)))
elif math == "tan":
self.display.insert(0, math.tan(float(second_number)))
elif math == "log":
self.display.insert(0, math.log(float(second_number)))
if self.mode == "programmer":
if math == "addition":
self.display.insert(0, hex(f_num + int(second_number)))
elif math == "sin":
self.display.insert(0, bin(int(math.sin(float(second_number)))))
def button_sin(self):
global math
math = "sin"
self.display.delete(0, tk.END)
def button_cos(self):
global math
math = "cos"
self.display.delete(0, tk.END)
def button_tan(self):
global math
math = "tan"
self.display.delete(0, tk.END)
def button_log(self):
global math
math = "log"
self.display.delete(0, tk.END)
def switch_mode(self, mode):
self.mode = mode
self.button_clear.invoke()
if mode == "standard":
self.create_standard_buttons()
elif mode == "scientific":
self.create_scientific_buttons()
elif mode == "programmer":
self.create_programmer_buttons()
# 启动计算器
root = tk.Tk()
calculator = Calculator(root)
mode_frame = tk.Frame(root)
mode_frame.grid(row=6, column=0, columnspan=4)
standard_button = tk.Button(mode_frame, text="Standard", command=lambda: calculator.switch_mode("standard"))
standard_button.pack(side="left")
scientific_button = tk.Button(mode_frame, text="Scientific", command=lambda: calculator.switch_mode("scientific"))
scientific_button.pack(side="left")
programmer_button = tk.Button(mode_frame, text="Programmer", command=lambda: calculator.switch_mode("programmer"))
programmer_button.pack(side="left")
root.mainloop()
```
这个计算器实现了标准、科学、程序员三种模式,可以在不同模式下进行数字、运算符、函数的输入和计算,并且使用了Tkinter库创建了一个简单的图形界面。你可以根据需要自定义不同功能的实现。
阅读全文