import math
class JSQ:
import tkinter
import math
def __init__(self):
# 设置一个用于检测运算符号的变量
self.yunsuan = False
# 用于保存内容的容器
self.save = []
self.root = self.tkinter.Tk()
self.root.title('新手版计算器')
self.root.minsize(300, 500)
self.showwid()
self.root.mainloop()
# 数字键
def showwid(self):
but1 = self.tkinter.Button(text='1', font=('黑体', 20), command=lambda: self.pressnum('1'))
but1.place(x=20, y=360, width=50, height=50)
but2 = self.tkinter.Button(text='2', font=('黑体', 20), command=lambda: self.pressnum('2'))
but2.place(x=90, y=360, width=50, height=50)
but3 = self.tkinter.Button(text='3', font=('黑体', 20), command=lambda: self.pressnum('3'))
but3.place(x=160, y=360, width=50, height=50)
but4 = self.tkinter.Button(text='4', font=('黑体', 20), command=lambda: self.pressnum('4'))
but4.place(x=20, y=290, width=50, height=50)
but5 = self.tkinter.Button(text='5', font=('黑体', 20), command=lambda: self.pressnum('5'))
but5.place(x=90, y=290, width=50, height=50)
but6 = self.tkinter.Button(text='6', font=('黑体', 20), command=lambda: self.pressnum('6'))
but6.place(x=160, y=290, width=50, height=50)
but7 = self.tkinter.Button(text='7', font=('黑体', 20), command=lambda: self.pressnum('7'))
but7.place(x=20, y=220, width=50, height=50)
but8 = self.tkinter.Button(text='8', font=('黑体', 20), command=lambda: self.pressnum('8'))
but8.place(x=90, y=220, width=50, height=50)
but9 = self.tkinter.Button(text='9', font=('黑体', 20), command=lambda: self.pressnum('9'))
but9.place(x=160, y=220, width=50, height=50)
but0 = self.tkinter.Button(text='0', font=('黑体', 20), command=lambda: self.pressnum('0'))
but0.place(x=20, y=430, width=50, height=50)
butdian = self.tkinter.Button(text='.', font=('黑体', 20), command=lambda: self.pressnum('.'))
butdian.place(x=90, y=430, width=50, height=50)
# 加减乘除键位
butjia = self.tkinter.Button(text='+', font=('黑体', 20), command=lambda: self.presssign('+'))
butjia.place(x=230, y=430, width=50, height=50)
butjian = self.tkinter.Button(text='-', font=('黑体', 20), command=lambda: self.presssign('-'))
butjian.place(x=230, y=360, width=50, height=50)
butcheng = self.tkinter.Button(text='*', font=('黑体', 20), command=lambda: self.presssign('*'))
butcheng.place(x=230, y=290, width=50, height=50)
butchu = self.tkinter.Button(text='/', font=('黑体', 15), command=lambda: self.presssign('/'))
butchu.place(x=230, y=220, width=50, height=50)
# 等于号
butequal = self.tkinter.Button(text='=', font=('黑体', 15), command=lambda: self.pressdy('='))
butequal.place(x=160, y=430, width=50, height=50)
# 标签最初的显示
self.labelnum = self.tkinter.StringVar()
self.labelnum.set('0')
# 屏幕标签
label = self.tkinter.Label(textvariable=self.labelnum, bg='#ffffff', anchor='se', font=('黑体', 20), bd=10)
label.place(x=20, y=20, width=260, height=90)
# 功能键
butback = self.tkinter.Button(text='←', font=('黑色', 20), command=self.backer)
butback.place(x=20, y=150, width=50, height=50)
butAC = self.tkinter.Button(text='AC', font=('黑色', 15), command=self.clearer)
butAC.place(x=90, y=150, width=50, height=50)
butzf = self.tkinter.Button(text='+/-', font=('黑色', 15), command=lambda: self.pressnum('+/-'))
butzf.place(x=160, y=150, width=50, height=50)
butgh = self.tkinter.Button(text='√', font=('黑色', 15),command= self.pressgh)
butgh.place(x=230, y=150, width=50, height=50)
# 按下数字键
def pressnum(self, num):
self.oldnum = self.labelnum.get()
if self.yunsuan ==True:
self.labelnum.set(num)
self.oldnum = ''
self.yunsuan = False
# 判断是否为正负号
if num == '+/-':
#判断是否是小数点结尾
if self.oldnum.endswith('.'):
self.labelnum.set(0 - int(self.oldnum[:-1]))
elif '.' in self.oldnum:
#判断数字是否为负数
if self.oldnum.startswith('-'):
self.labelnum.set(self.labelnum.get()[1:])
else:
self.labelnum.set('-' + self.labelnum.get())
else:
self.labelnum.set(0 - int(self.oldnum))
# 判断是否为小数点
elif num == '.':
# 判断是否为浮点数
if num in self.oldnum:
pass
else:
self.labelnum.set(self.oldnum + num)
elif self.oldnum == '0':
self.labelnum.set(num)
else:
self.labelnum.set(self.oldnum + num)
# 按下运算符号
def presssign(self,sign):
oldnum = self.labelnum.get()
self.save.append(oldnum)
self.save.append(sign)
print(self.save)
self.yunsuan = True
# 判断是否为根号
def pressgh(self):
oldnum = self.labelnum.get()
if oldnum.startswith('-') == False:
print(oldnum)
result = self.labelnum.set(str(math.sqrt(float(oldnum)))[:12])
else:
if oldnum.startswith('-') == True:
result = self.labelnum.set('输入有误')
# 按下等于符号
def pressdy(self,dy):
# 将字符串转为函数进行运算
self.save.append(self.labelnum.get())
print(self.save)
try:
result = round(eval(''.join(self.save)), 8)
except:
self.labelnum.set('输入有误')
else:
self.labelnum.set(result)
self.yunsuan = True
self.save.clear()
# 按下返回键
def backer(self):
self.labelnum.set(self.labelnum.get()[:-1])
# 按下清空键
def clearer(self):
self.labelnum.set(0)
self.save.clear()
jsq = JSQ()
Python学习交流、资源共享群:563626388 QQ