类似项目 👁
项目背景??
???????本人已经做了两个基于Python的计算器项目,有需要的话,请看本文头部。这次基于Python制作一个科学计算器 项目,创建一个科学计算器。
项目需求💥
1、科学计算器应包括:计算器界面、0-9数字按键、清零和各功能按键; 2、功能1:二进制、八进制、十进制、十六进制、四种进制可相互切换; 3、功能2:加减乘除基本运算、平方、开方、x^y、 x^3等; 4、功能3:tan、cos、sin、n!、log等
代码展示💻
from tkinter import StringVar, Entry, Button
from tkinter import Tk
import math
class calculator:
def __init__(self):
window = Tk()
window.title('基于TK的科学计算器')
window.configure(background="white")
self.string = StringVar()
entry = Entry(window, textvariable=self.string, )
entry.grid(row=0, column=0, columnspan=6)
entry.configure(background="white")
entry.focus()
values = ["C", "DEL", "(", ")", "%", "!",
"sin", "sqrt", "e", "pow", "/", "oct",
"cos", "7", "8", "9", "*", "int",
"tan", "4", "5", "6", "-", "hex",
"pi", "1", "2", "3", "+", "bin",
"log", ",", "0", ".", "="]
text = 1
i = 0
row = 1
col = 0
for txt in values:
padx = 10
pady = 10
if (i == 6):
row = 2
col = 0
if (i == 12):
row = 3
col = 0
if (i == 18):
row = 4
col = 0
if (i == 24):
row = 5
col = 0
if (i == 30):
row = 6
col = 0
if (txt == '='):
btn = Button(window, height=2, width=4, padx=50, pady=pady, text=txt,
command=lambda txt=txt: self.equals())
btn.grid(row=row, column=col, columnspan=3, padx=2, pady=2)
btn.configure(background="yellow")
elif (txt == 'DEL'):
btn = Button(window, height=2, width=4, padx=padx, pady=pady, text=txt,
command=lambda txt=txt: self.delete())
btn.grid(row=row, column=col, padx=1, pady=1)
btn.configure(background="grey")
elif (txt == 'C'):
btn = Button(window, height=2, width=4, padx=padx, pady=pady, text=txt,
command=lambda txt=txt: self.clearall())
btn.grid(row=row, column=col, padx=1, pady=1)
btn.configure(background="red")
else:
btn = Button(window, height=2, width=4, padx=padx, pady=pady, text=txt,
command=lambda txt=txt: self.addChar(txt))
btn.grid(row=row, column=col, padx=1, pady=1)
btn.configure(background="white")
col = col + 1
i = i + 1
window.mainloop()
def fac(self):
n = int(self.string.get()[0:1])
print(n)
num = 1
if n < 0:
print('负数没有阶乘!')
elif n == 0:
print('0的阶乘为1!')
else:
for i in range(1, n + 1):
num *= i
return self.string.set(num);
def clearall(self):
self.string.set("")
def equals(self):
result = ""
try:
if self.string.get().find('!')==True:
self.fac()
result = eval(self.string.get())
self.string.set(result)
except:
result = "无效输入"
self.string.set(result)
def addChar(self, char):
i = ['log', 'sqrt', 'pi', 'sin', 'cos', 'tan', 'e', "gcd", "radians", "degrees", "ceil", "hypot"]
if char in i:
self.string.set(self.string.get() + 'math.' + (str(char)))
print("addchar的if语句"+self.string.get().format(()) + " " + str(char))
else:
self.string.set(self.string.get() + (str(char)))
print("addchar的else语句"+self.string.get() + " " + str(char))
def delete(self):
self.string.set(self.string.get()[0:-1])
calculator()
源码下载 🚗
点击这段文字,可以下载源码!如果有帮助到你,欢迎打赏!
|