Python简单实现计算器
最近刚刚学完python基础,就想着自己试试编写一个计算器来。还有很多不足的地方大家可以交流一下。
from tkinter import *
class calculator:
def __init__(self,root):
self.v1 = StringVar()
self.v1.set(0)
self.iscal = False#设置一个变量判断是否按下运算键
#设置两个frame来存放输入框和按键
frame1 = Frame(root)
frame1.pack()
frame2 = Frame(root)
frame2.pack()
e1 = Entry(frame1, width=32, textvariable=self.v1, state='readonly')
e1.grid(row=0, column=1)
Button(frame2, text='1',height=3,width=5,command=lambda: self.numbutton('1')).grid(row=1, column=0)
Button(frame2, text='2', height=3,width=5,command=lambda: self.numbutton('2')).grid(row=1, column=1)
Button(frame2, text='3', height=3,width=5,command=lambda: self.numbutton('3')).grid(row=1, column=2)
Button(frame2, text='4', height=3,width=5,command=lambda: self.numbutton('4')).grid(row=2, column=0)
Button(frame2, text='5', height=3,width=5,command=lambda: self.numbutton('5')).grid(row=2, column=1)
Button(frame2, text='6', height=3,width=5,command=lambda: self.numbutton('6')).grid(row=2, column=2)
Button(frame2, text='7', height=3,width=5,command=lambda: self.numbutton('7')).grid(row=3, column=0)
Button(frame2, text='8', height=3,width=5,command=lambda: self.numbutton('8')).grid(row=3, column=1)
Button(frame2, text='9', height=3,width=5,command=lambda: self.numbutton('9')).grid(row=3, column=2)
Button(frame2, text='0', height=3,width=5,command=lambda: self.numbutton('0')).grid(row=4, column=1)
Button(frame2, text='+', height=3,width=5,command=lambda: self.calbutton('+')).grid(row=1, column=3)
Button(frame2, text='-', height=3,width=5,command=lambda: self.calbutton('-')).grid(row=2, column=3)
Button(frame2, text='*', height=3,width=5,command=lambda: self.calbutton('*')).grid(row=3, column=3)
Button(frame2, text='/', height=3,width=5,command=lambda: self.calbutton('/')).grid(row=4, column=3)
Button(frame2, text='=', height=3,width=5,command=lambda: self.calc()).grid(row=4, column=4)
Button(frame2, text='C', height=3,width=5,command=lambda: self.clear()).grid(row=4, column=0)
Button(frame2, text='.', height=3, width=5, command=lambda: self.calbutton('.')).grid(row=4, column=2)
Button(frame2, text='M+', height=3, width=5, command=lambda: self.new()).grid(row=3, column=4)
Button(frame2, text='M-', height=3, width=5, command=lambda: self.new()).grid(row=2, column=4)
Button(frame2, text='<-', height=3, width=5, command=lambda: self.dele()).grid(row=1, column=4)
#输入数字的函数
def numbutton(self,num):
oldnum = self.v1.get()
if oldnum == '0':
self.v1.set(num)
else:
newnum = oldnum + num
self.v1.set(newnum)
#输入运算符的函数
def calbutton(self,cal):
if self.iscal == False:
self.v1.set(self.v1.get()+cal)
self.iscal = True
else:
if self.v1.get()[-1] in ['+','-','*','/','.']:
#v1.get[-1]用来获取v1.get的最后一个值
self.v1.set(self.v1.get()[:-1])
#v1.get()[:-1]用来获取除了最后一个值以外的值
self.v1.set(self.v1.get() + cal)
else:
self.v1.set(self.v1.get() + cal)
#计算函数
def calc(self):
self.v1.set(self.v1.get()+'='+str(eval(self.v1.get())))
#eval()可以直接计算str内的计算式
#清除函数
def clear(self):
self.v1.set(0)
#尚未开发的函数
def new(self):
pass
#删除单个字符串
def dele(self):
self.v1.set(self.v1.get()[:-1])
root = Tk()
cal = calculator(root)
mainloop()