帮我编写一个拥有精美图形界面的科学计算器python程序
时间: 2023-02-22 15:49:08 浏览: 123
Pangolin,有图形界面c语言编写科学计算器源码,c语言
可以使用 GUI 库如 tkinter 来编写一个拥有精美图形界面的科学计算器。
以下是一个使用 tkinter 编写的简单科学计算器的示例代码:
```
import tkinter as tk
from math import pi, e, sin, cos, tan, log10, log, sqrt
class Calculator:
def __init__(self, master):
self.master = master
master.title("科学计算器")
self.result = tk.StringVar()
self.result.set("0")
self.result_label = tk.Label(master, textvariable=self.result, font=("Arial", 16), anchor="e")
self.result_label.grid(row=0, column=0, columnspan=4, padx=10, pady=10, ipady=10, sticky="we")
self.create_buttons()
def create_buttons(self):
values = [
["π", "e", "x!", "C"],
["7", "8", "9", "/"],
["4", "5", "6", "*"],
["1", "2", "3", "-"],
["0", ".", "=", "+"],
]
for i, value_list in enumerate(values):
for j, value in enumerate(value_list):
if value == "=":
btn = tk.Button(self.master, text=value, font=("Arial", 16), command=self.calculate)
elif value == "C":
btn = tk.Button(self.master, text=value, font=("Arial", 16), command=self.clear)
else:
btn = tk.Button(self.master, text=value, font=("Arial", 16), command=lambda x=value: self.insert(x))
btn.grid(row=i + 1, column=j, padx=10, pady=10)
def clear(self):
self.result.set("0")
def insert(self, value):
current = self.result.get()
if current == "0":
self.result.set(value)
else:
self.result.set(current + value)
def calculate(self):
expression = self.result.get()
try:
self.result.set(eval(expression))
except:
self.result.set("Error")
root = tk.Tk()
app = Calculator(root)
root.mainloop()
```
使用这段代码,你可以创建一个简单的科学计算器。然而
阅读全文