用tkinter包写个计算器 包括程序员计算器和科学计算器功能
时间: 2023-08-15 20:05:03 浏览: 150
Python编程使用tkinter模块实现计算器软件完整代码示例
5星 · 资源好评率100%
好的,下面是用tkinter包写的计算器代码,包括程序员计算器和科学计算器功能。
```python
from tkinter import *
import math
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
master.geometry("400x450")
# Create entry widget
self.display = Entry(master, width=50, borderwidth=5)
self.display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# Create buttons
self.button_0 = Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
self.button_1 = Button(master, text="1", padx=40, pady=20, command=lambda: self.button_click(1))
self.button_2 = Button(master, text="2", padx=40, pady=20, command=lambda: self.button_click(2))
self.button_3 = Button(master, text="3", padx=40, pady=20, command=lambda: self.button_click(3))
self.button_4 = Button(master, text="4", padx=40, pady=20, command=lambda: self.button_click(4))
self.button_5 = Button(master, text="5", padx=40, pady=20, command=lambda: self.button_click(5))
self.button_6 = Button(master, text="6", padx=40, pady=20, command=lambda: self.button_click(6))
self.button_7 = Button(master, text="7", padx=40, pady=20, command=lambda: self.button_click(7))
self.button_8 = Button(master, text="8", padx=40, pady=20, command=lambda: self.button_click(8))
self.button_9 = Button(master, text="9", padx=40, pady=20, command=lambda: self.button_click(9))
self.button_add = Button(master, text="+", padx=39, pady=20, command=self.button_add)
self.button_subtract = Button(master, text="-", padx=41, pady=20, command=self.button_subtract)
self.button_multiply = Button(master, text="*", padx=40, pady=20, command=self.button_multiply)
self.button_divide = Button(master, text="/", padx=41, pady=20, command=self.button_divide)
self.button_equal = Button(master, text="=", padx=91, pady=20, command=self.button_equal)
self.button_clear = Button(master, text="Clear", padx=79, pady=20, command=self.button_clear)
self.button_decimal = Button(master, text=".", padx=41, pady=20, command=self.button_decimal)
self.button_pi = Button(master, text="π", padx=39, pady=20, command=self.button_pi)
self.button_sin = Button(master, text="sin", padx=36, pady=20, command=self.button_sin)
self.button_cos = Button(master, text="cos", padx=36, pady=20, command=self.button_cos)
self.button_tan = Button(master, text="tan", padx=36, pady=20, command=self.button_tan)
self.button_log = Button(master, text="log", padx=36, pady=20, command=self.button_log)
self.button_exp = Button(master, text="exp", padx=36, pady=20, command=self.button_exp)
self.button_bin = Button(master, text="bin", padx=36, pady=20, command=self.button_bin)
self.button_oct = Button(master, text="oct", padx=36, pady=20, command=self.button_oct)
self.button_hex = Button(master, text="hex", padx=36, pady=20, command=self.button_hex)
# Put buttons on the screen
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_decimal.grid(row=4, column=2)
self.button_equal.grid(row=5, column=1, columnspan=2)
self.button_pi.grid(row=1, column=3)
self.button_sin.grid(row=2, column=3)
self.button_cos.grid(row=3, column=3)
self.button_tan.grid(row=4, column=3)
self.button_log.grid(row=5, column=3)
self.button_exp.grid(row=6, column=3)
self.button_bin.grid(row=7, column=0)
self.button_oct.grid(row=7, column=1)
self.button_hex.grid(row=7, column=2)
def button_click(self, number):
current = self.display.get()
self.display.delete(0, END)
self.display.insert(0, str(current) + str(number))
def button_add(self):
first_number = self.display.get()
self.master.first_number = float(first_number)
self.master.operation = "+"
self.display.delete(0, END)
def button_subtract(self):
first_number = self.display.get()
self.master.first_number = float(first_number)
self.master.operation = "-"
self.display.delete(0, END)
def button_multiply(self):
first_number = self.display.get()
self.master.first_number = float(first_number)
self.master.operation = "*"
self.display.delete(0, END)
def button_divide(self):
first_number = self.display.get()
self.master.first_number = float(first_number)
self.master.operation = "/"
self.display.delete(0, END)
def button_clear(self):
self.display.delete(0, END)
def button_decimal(self):
current = self.display.get()
if "." not in current:
self.display.insert(END, ".")
def button_equal(self):
second_number = self.display.get()
self.display.delete(0, END)
if self.master.operation == "+":
self.display.insert(0, self.master.first_number + float(second_number))
elif self.master.operation == "-":
self.display.insert(0, self.master.first_number - float(second_number))
elif self.master.operation == "*":
self.display.insert(0, self.master.first_number * float(second_number))
elif self.master.operation == "/":
self.display.insert(0, self.master.first_number / float(second_number))
def button_pi(self):
self.display.delete(0, END)
self.display.insert(0, math.pi)
def button_sin(self):
angle = float(self.display.get())
self.display.delete(0, END)
self.display.insert(0, math.sin(angle))
def button_cos(self):
angle = float(self.display.get())
self.display.delete(0, END)
self.display.insert(0, math.cos(angle))
def button_tan(self):
angle = float(self.display.get())
self.display.delete(0, END)
self.display.insert(0, math.tan(angle))
def button_log(self):
number = float(self.display.get())
self.display.delete(0, END)
self.display.insert(0, math.log10(number))
def button_exp(self):
number = float(self.display.get())
self.display.delete(0, END)
self.display.insert(0, math.exp(number))
def button_bin(self):
number = int(self.display.get())
self.display.delete(0, END)
self.display.insert(0, bin(number))
def button_oct(self):
number = int(self.display.get())
self.display.delete(0, END)
self.display.insert(0, oct(number))
def button_hex(self):
number = int(self.display.get())
self.display.delete(0, END)
self.display.insert(0, hex(number))
root = Tk()
my_calculator = Calculator(root)
root.mainloop()
```
这个计算器包括基本的加、减、乘、除、小数点、清除和等于功能,以及一些科学计算器的功能,例如正弦、余弦、正切、对数、指数、二进制、八进制和十六进制转换。
你可以在窗口中输入数字,然后按运算符进行计算。如果你想进行科学计算,可以按下相应的按钮,例如sin、cos、tan或log。此外,还有一个π按钮,可以插入π的值。
阅读全文