资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

基于pythontkinter的简单计算器(v1.0)-创新互联

import tkinter

#定义计算器类
class Calc:
	#初始化魔术方法
	def __init__(self):
		#初始化共用属性
		
		#定义一个用于存放被计算字符串的列表
		self.operationList = []

		#定义运算标记 确定是否输入了运算符号
		self.isOper = False

		#初始化界面
		self.initWindows()

	#更改按键盘颜色方法
	def changeBg(self,evt):
		evt.widget['bg'] = 'cyan'

	#恢复按键盘颜色方法
	def backBg(self,evt):
		evt.widget['bg'] = 'lightgray'

	#数字按钮操作方法
	def buttonAction(self,number):
		#判断用户是否按下了运算按钮
		if self.isOper == True:
			#在界面上显示运算符之后的数
			self.num.set(number)
			#运算标记复位
			self.isOper = False
		else:
			#没有铵下运算按钮
			#判断原始界面数字是否为0
			existNumber = self.num.get()
			if existNumber == '0':
				#如果界面中的初始数据为0 则获取用户输入数据并显示
				self.num.set(number)
			else:
				#如果界面中的初始数据不为0 则对字符进行累加
				self.num.set(self.num.get()+number)

	#运算按钮操作方法 
	def operation(self,opFlag):
		#运算标记置为真
		self.isOper = True
		#获取界面中存在的数 并且写入列表
		self.operationList.append(self.num.get())
		#当前运算符号不会在上一步中写入 需要单独写入
		self.operationList.append(opFlag)
		
	#获取运行结果操作方法 
	def getResult(self):
		#将当前界面中数字加入计算列表
		self.operationList.append(self.num.get())

		#开始计算 
		result = eval(''.join(self.operationList))
		self.num.set(result)

	#全部清空重新计算方法 
	def clearAll(self):
		#界面置0 计算列表置0
		self.num.set('0')
		self.operationList.clear()
		#运算标志复位
		self.isOper = False

	#实现退格键方法
	def backSpace(self):
		#获取当前显示数字长度
		strLength = len(self.num.get())
		#如果当前显示有数字
		if strLength > 1:
			#删除字串中最后一个字
			presentStr = self.num.get()
			presentStr = presentStr[:strLength - 1]
			self.num.set(presentStr)
		else:
			self.num.set('0')

	#正负号实现方法
	def pm(self):
		presentStr = self.num.get()
		#实现增加和去除负号
		if presentStr[0] == '-':
			self.num.set(presentStr[1:])
			#原始字串不得以-号和0开头
		elif presentStr[0] not in ('-','0'):
			self.num.set('-'+presentStr)

	#界面布局方法
	def initWindows(self):
		#生成主窗口 定制窗口尺寸
		root = tkinter.Tk()
		root.minsize(400,500)
		root.title('微硬计算器')

		#生成用于保存数值的变量
		self.num = tkinter.StringVar()
		self.num.set(0)

		#运算结果输出位置
		result = tkinter.Label(root,width=20,height=2,bg='white',bd=10,anchor='e',font=('宋体',50),textvariable=self.num)

		result.place(relx=0,rely=0,relwidth=1.0,relheight=0.4)

		###########################以下为按键部分############################
		buttonCE = tkinter.Button(root,text='CE',bg='lightgray',command = self.clearAll)
		buttonCE.place(relx=0,rely=0.4,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonCE.bind('',self.changeBg)
		buttonCE.bind('',self.backBg)

		buttonC = tkinter.Button(root,text='C',bg='lightgray',command = self.clearAll)
		buttonC.place(relx=0.25,rely=0.4,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonC.bind('',self.changeBg)
		buttonC.bind('',self.backBg)

		buttonDel = tkinter.Button(root,text='<-',bg='lightgray',command = self.backSpace)
		buttonDel.place(relx=0.5,rely=0.4,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonDel.bind('',self.changeBg)
		buttonDel.bind('',self.backBg)

		buttonDiv = tkinter.Button(root,text='÷',bg='lightgray',command = lambda : self.operation('/'))
		buttonDiv.place(relx=0.75,rely=0.4,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonDiv.bind('',self.changeBg)
		buttonDiv.bind('',self.backBg)

		button1 = tkinter.Button(root,text='1',bg='lightgray',command = lambda : self.buttonAction('1'))
		button1.place(relx=0,rely=0.5,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button1.bind('',self.changeBg)
		button1.bind('',self.backBg)

		button2 = tkinter.Button(root,text='2',bg='lightgray',command = lambda : self.buttonAction('2'))
		button2.place(relx=0.25,rely=0.5,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button2.bind('',self.changeBg)
		button2.bind('',self.backBg)

		button3 = tkinter.Button(root,text='3',bg='lightgray',command = lambda : self.buttonAction('3'))
		button3.place(relx=0.5,rely=0.5,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button3.bind('',self.changeBg)
		button3.bind('',self.backBg)

		buttonX = tkinter.Button(root,text='x',bg='lightgray',command = lambda : self.operation('*'))
		buttonX.place(relx=0.75,rely=0.5,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonX.bind('',self.changeBg)
		buttonX.bind('',self.backBg)

		button4 = tkinter.Button(root,text='4',bg='lightgray',command = lambda : self.buttonAction('4'))
		button4.place(relx=0,rely=0.6,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button4.bind('',self.changeBg)
		button4.bind('',self.backBg)

		button5 = tkinter.Button(root,text='5',bg='lightgray',command = lambda : self.buttonAction('5'))
		button5.place(relx=0.25,rely=0.6,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button5.bind('',self.changeBg)
		button5.bind('',self.backBg)

		button6 = tkinter.Button(root,text='6',bg='lightgray',command = lambda : self.buttonAction('6'))
		button6.place(relx=0.5,rely=0.6,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button6.bind('',self.changeBg)
		button6.bind('',self.backBg)

		button_ = tkinter.Button(root,text='-',bg='lightgray',command = lambda : self.operation('-'))
		button_.place(relx=0.75,rely=0.6,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button_.bind('',self.changeBg)
		button_.bind('',self.backBg)

		button7 = tkinter.Button(root,text='7',bg='lightgray',command = lambda : self.buttonAction('7'))
		button7.place(relx=0,rely=0.7,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button7.bind('',self.changeBg)
		button7.bind('',self.backBg)

		button8 = tkinter.Button(root,text='8',bg='lightgray',command = lambda : self.buttonAction('8'))
		button8.place(relx=0.25,rely=0.7,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button8.bind('',self.changeBg)
		button8.bind('',self.backBg)

		button9 = tkinter.Button(root,text='9',bg='lightgray',command = lambda : self.buttonAction('9'))
		button9.place(relx=0.5,rely=0.7,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button9.bind('',self.changeBg)
		button9.bind('',self.backBg)

		buttonAdd = tkinter.Button(root,text='+',bg='lightgray',command = lambda : self.operation('+'))
		buttonAdd.place(relx=0.75,rely=0.7,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonAdd.bind('',self.changeBg)
		buttonAdd.bind('',self.backBg)

		buttonFlag = tkinter.Button(root,text='±',bg='lightgray',command = self.pm)
		buttonFlag.place(relx=0,rely=0.8,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonFlag.bind('',self.changeBg)
		buttonFlag.bind('',self.backBg)

		button0 = tkinter.Button(root,text='0',bg='lightgray',command = lambda : self.buttonAction('0'))
		button0.place(relx=0.25,rely=0.8,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		button0.bind('',self.changeBg)
		button0.bind('',self.backBg)

		buttonPoint = tkinter.Button(root,text='.',bg='lightgray',command = lambda : self.buttonAction('.'))
		buttonPoint.place(relx=0.5,rely=0.8,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonPoint.bind('',self.changeBg)
		buttonPoint.bind('',self.backBg)

		buttonEque = tkinter.Button(root,text='=',bg='lightgray',command = self.getResult)
		buttonEque.place(relx=0.75,rely=0.8,relwidth=0.25,relheight=0.1)
		#绑定按钮 生成鼠标经过变色效果
		buttonEque.bind('',self.changeBg)
		buttonEque.bind('',self.backBg)
		#########################以上为按键部分############################
		#底部显示信息
		bottomLabel = tkinter.Label(root,text = 'Power By Microhard Corpration\n@2017'
		,bg='cyan',width=30,height = 1,padx=0)
		bottomLabel.place(relx=0,rely=0.9,relwidth=1.0,relheight=0.1)

		#主窗口循环
		root.mainloop()


            
#实例化计算器对象
c = Calc()

10年积累的成都网站建设、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有鄂托克前免费网站建设让你可以放心的选择与我们合作。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:基于pythontkinter的简单计算器(v1.0)-创新互联
网页链接:http://cdkjz.cn/article/esocs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220