使用tkinter库实现一个科学计算器。要求至少能实现加运算、减运算、乘运算、除运算、幂运算和取余运算。
时间: 2023-07-10 08:25:01 浏览: 157
如何利用python的tkinter实现一个简单的计算器.docx
好的,下面是一个简单的科学计算器示例,使用tkinter库实现,支持加、减、乘、除、幂和取余运算:
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
master.title("科学计算器")
# 创建输入框
self.display = tk.Entry(master, width=40, 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_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_power = tk.Button(master, text="^", padx=40, pady=20, command=self.button_power)
self.button_modulo = tk.Button(master, text="%", padx=39, pady=20, command=self.button_modulo)
self.button_clear = tk.Button(master, text="清除", padx=79, pady=20, command=self.button_clear)
self.button_equals = tk.Button(master, text="=", padx=91, pady=20, command=self.button_equals)
# 将按钮添加到窗口
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_power.grid(row=5, column=1)
self.button_modulo.grid(row=5, column=2)
self.button_equals.grid(row=4, column=3, columnspan=2)
self.current_operation = None
self.first_number = None
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)
self.current_operation = None
self.first_number = None
def button_add(self):
self.current_operation = "add"
self.first_number = float(self.display.get())
self.display.delete(0, tk.END)
def button_subtract(self):
self.current_operation = "subtract"
self.first_number = float(self.display.get())
self.display.delete(0, tk.END)
def button_multiply(self):
self.current_operation = "multiply"
self.first_number = float(self.display.get())
self.display.delete(0, tk.END)
def button_divide(self):
self.current_operation = "divide"
self.first_number = float(self.display.get())
self.display.delete(0, tk.END)
def button_power(self):
self.current_operation = "power"
self.first_number = float(self.display.get())
self.display.delete(0, tk.END)
def button_modulo(self):
self.current_operation = "modulo"
self.first_number = float(self.display.get())
self.display.delete(0, tk.END)
def button_equals(self):
second_number = float(self.display.get())
self.display.delete(0, tk.END)
if self.current_operation == "add":
result = self.first_number + second_number
elif self.current_operation == "subtract":
result = self.first_number - second_number
elif self.current_operation == "multiply":
result = self.first_number * second_number
elif self.current_operation == "divide":
result = self.first_number / second_number
elif self.current_operation == "power":
result = self.first_number ** second_number
elif self.current_operation == "modulo":
result = self.first_number % second_number
else:
result = 0
self.display.insert(0, result)
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
您可以在此基础上进行修改和扩展以适应您的需求。
阅读全文