怎么用tkinter做出一个科学计算器
时间: 2024-05-15 22:19:39 浏览: 41
python使用tkinter实现简单计算器
以下是一个使用tkinter实现的简单科学计算器:
```python
import tkinter as tk
import math
# 创建主窗口
root = tk.Tk()
root.title("科学计算器")
# 创建显示屏
display = tk.Entry(root, width=40, justify="right")
display.grid(row=0, column=0, columnspan=5, pady=5)
# 操作函数
def button_click(number):
current = display.get()
display.delete(0, tk.END)
display.insert(0, str(current) + str(number))
def button_clear():
display.delete(0, tk.END)
def button_equal():
try:
result = eval(display.get())
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
def button_sqrt():
try:
result = math.sqrt(float(display.get()))
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
def button_sin():
try:
result = math.sin(math.radians(float(display.get())))
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
def button_cos():
try:
result = math.cos(math.radians(float(display.get())))
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
def button_tan():
try:
result = math.tan(math.radians(float(display.get())))
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
# 创建按钮
button_1 = tk.Button(root, text="1", padx=20, pady=10, command=lambda: button_click(1))
button_2 = tk.Button(root, text="2", padx=20, pady=10, command=lambda: button_click(2))
button_3 = tk.Button(root, text="3", padx=20, pady=10, command=lambda: button_click(3))
button_4 = tk.Button(root, text="4", padx=20, pady=10, command=lambda: button_click(4))
button_5 = tk.Button(root, text="5", padx=20, pady=10, command=lambda: button_click(5))
button_6 = tk.Button(root, text="6", padx=20, pady=10, command=lambda: button_click(6))
button_7 = tk.Button(root, text="7", padx=20, pady=10, command=lambda: button_click(7))
button_8 = tk.Button(root, text="8", padx=20, pady=10, command=lambda: button_click(8))
button_9 = tk.Button(root, text="9", padx=20, pady=10, command=lambda: button_click(9))
button_0 = tk.Button(root, text="0", padx=20, pady=10, command=lambda: button_click(0))
button_dot = tk.Button(root, text=".", padx=22, pady=10, command=lambda: button_click("."))
button_plus = tk.Button(root, text="+", padx=19, pady=10, command=lambda: button_click("+"))
button_minus = tk.Button(root, text="-", padx=20, pady=10, command=lambda: button_click("-"))
button_multiply = tk.Button(root, text="*", padx=20, pady=10, command=lambda: button_click("*"))
button_divide = tk.Button(root, text="/", padx=20, pady=10, command=lambda: button_click("/"))
button_clear = tk.Button(root, text="C", padx=19, pady=10, command=button_clear)
button_equal = tk.Button(root, text="=", padx=67, pady=10, command=button_equal)
button_sqrt = tk.Button(root, text="√", padx=20, pady=10, command=button_sqrt)
button_sin = tk.Button(root, text="sin", padx=17, pady=10, command=button_sin)
button_cos = tk.Button(root, text="cos", padx=16, pady=10, command=button_cos)
button_tan = tk.Button(root, text="tan", padx=16, pady=10, command=button_tan)
# 按钮位置
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=1)
button_dot.grid(row=4, column=0)
button_plus.grid(row=4, column=3)
button_minus.grid(row=3, column=3)
button_multiply.grid(row=2, column=3)
button_divide.grid(row=1, column=3)
button_clear.grid(row=4, column=2)
button_equal.grid(row=5, column=0, columnspan=4)
button_sqrt.grid(row=5, column=2)
button_sin.grid(row=6, column=0)
button_cos.grid(row=6, column=1)
button_tan.grid(row=6, column=2)
# 运行主程序
root.mainloop()
```
在这个计算器中,我们使用了eval()函数来计算表达式的值。eval()函数将字符串作为输入,返回表达式的计算结果。此函数非常强大,因此在实际应用中应该注意安全性问题。此外,我们还使用了Python的math库来实现平方根、正弦、余弦和正切等函数。
阅读全文