用tkinter包完成一个类似于计算器的程序,包括程序员计算器功能,科学计算器功能
时间: 2024-03-26 15:41:30 浏览: 56
好的,我可以给您提供一个简单的tkinter程序,其中包含程序员计算器和科学计算器的功能。代码如下:
```python
import tkinter as tk
import math
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
# 创建一个输入框
self.entry = tk.Entry(master, width=50, borderwidth=5)
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 创建按钮
# 第一行
self.button_clear = tk.Button(master, text="C", padx=40, pady=20, command=self.clear)
self.button_programmer = tk.Button(master, text="Programmer", padx=50, pady=20, command=self.programmer)
self.button_power = tk.Button(master, text="^", padx=41, pady=20, command=lambda: self.button_click("**"))
self.button_divide = tk.Button(master, text="/", padx=41, pady=20, command=lambda: self.button_click("/"))
# 第二行
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_multiply = tk.Button(master, text="*", padx=41, pady=20, command=lambda: self.button_click("*"))
# 第三行
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_subtract = tk.Button(master, text="-", padx=41, pady=20, command=lambda: self.button_click("-"))
# 第四行
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_add = tk.Button(master, text="+", padx=41, pady=20, command=lambda: self.button_click("+"))
# 第五行
self.button_0 = tk.Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
self.button_decimal = tk.Button(master, text=".", padx=41, pady=20, command=lambda: self.button_click("."))
self.button_equal = tk.Button(master, text="=", padx=91, pady=20, command=self.equal)
# 将按钮放入网格中
# 第一行
self.button_clear.grid(row=1, column=0)
self.button_programmer.grid(row=1, column=1)
self.button_power.grid(row=1, column=2)
self.button_divide.grid(row=1, column=3)
# 第二行
self.button_7.grid(row=2, column=0)
self.button_8.grid(row=2, column=1)
self.button_9.grid(row=2, column=2)
self.button_multiply.grid(row=2, column=3)
# 第三行
self.button_4.grid(row=3, column=0)
self.button_5.grid(row=3, column=1)
self.button_6.grid(row=3, column=2)
self.button_subtract.grid(row=3, column=3)
# 第四行
self.button_1.grid(row=4, column=0)
self.button_2.grid(row=4, column=1)
self.button_3.grid(row=4, column=2)
self.button_add.grid(row=4, column=3)
# 第五行
self.button_0.grid(row=5, column=0)
self.button_decimal.grid(row=5, column=1)
self.button_equal.grid(row=5, column=2, columnspan=2)
self.math_constants = {
"π": math.pi,
"e": math.e,
"φ": (1 + math.sqrt(5)) / 2 # 黄金分割比例
}
self.programmer_mode = False
def button_click(self, number):
current = self.entry.get()
self.entry.delete(0, tk.END)
self.entry.insert(0, str(current) + str(number))
def clear(self):
self.entry.delete(0, tk.END)
def equal(self):
try:
if self.programmer_mode:
expression = self.entry.get()
expression = expression.replace("0b", "0").replace("0x", "0")
result = eval(expression)
self.entry.delete(0, tk.END)
self.entry.insert(0, bin(result)[2:] if result >= 0 else "-" + bin(-result)[2:])
else:
expression = self.entry.get()
expression = expression.replace("^", "**")
for constant, value in self.math_constants.items():
expression = expression.replace(constant, str(value))
result = eval(expression)
self.entry.delete(0, tk.END)
self.entry.insert(0, result)
except:
self.entry.delete(0, tk.END)
self.entry.insert(0, "Error")
def programmer(self):
self.programmer_mode = not self.programmer_mode
if self.programmer_mode:
self.button_programmer.config(text="Scientific")
self.button_power.config(state="disabled")
self.button_divide.config(text="//")
self.button_multiply.config(text="Mod")
self.math_constants["∞"] = float("inf")
else:
self.button_programmer.config(text="Programmer")
self.button_power.config(state="normal")
self.button_divide.config(text="/")
self.button_multiply.config(text="*")
self.math_constants.pop("∞", None)
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
在这个程序中,我们创建了一个名为Calculator的类,并在其中使用了tkinter构建了一个简单的计算器。程序员计算器模式和科学计算器模式之间可以切换。在程序员计算器模式下,我们可以进行二进制和十六进制的运算。在科学计算器模式下,我们可以使用各种常量和函数,如π和sin()等。
阅读全文