溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于python tkinter的簡單計(jì)算器(v1.0)

發(fā)布時(shí)間:2020-09-15 12:33:13 來源:網(wǎng)絡(luò) 閱讀:1573 作者:大陌 欄目:軟件技術(shù)
import tkinter

#定義計(jì)算器類
class Calc:
	#初始化魔術(shù)方法
	def __init__(self):
		#初始化共用屬性
		
		#定義一個(gè)用于存放被計(jì)算字符串的列表
		self.operationList = []

		#定義運(yùn)算標(biāo)記 確定是否輸入了運(yùn)算符號
		self.isOper = False

		#初始化界面
		self.initWindows()

	#更改按鍵盤顏色方法
	def changeBg(self,evt):
		evt.widget['bg'] = 'cyan'

	#恢復(fù)按鍵盤顏色方法
	def backBg(self,evt):
		evt.widget['bg'] = 'lightgray'

	#數(shù)字按鈕操作方法
	def buttonAction(self,number):
		#判斷用戶是否按下了運(yùn)算按鈕
		if self.isOper == True:
			#在界面上顯示運(yùn)算符之后的數(shù)
			self.num.set(number)
			#運(yùn)算標(biāo)記復(fù)位
			self.isOper = False
		else:
			#沒有銨下運(yùn)算按鈕
			#判斷原始界面數(shù)字是否為0
			existNumber = self.num.get()
			if existNumber == '0':
				#如果界面中的初始數(shù)據(jù)為0 則獲取用戶輸入數(shù)據(jù)并顯示
				self.num.set(number)
			else:
				#如果界面中的初始數(shù)據(jù)不為0 則對字符進(jìn)行累加
				self.num.set(self.num.get()+number)

	#運(yùn)算按鈕操作方法 
	def operation(self,opFlag):
		#運(yùn)算標(biāo)記置為真
		self.isOper = True
		#獲取界面中存在的數(shù) 并且寫入列表
		self.operationList.append(self.num.get())
		#當(dāng)前運(yùn)算符號不會在上一步中寫入 需要單獨(dú)寫入
		self.operationList.append(opFlag)
		
	#獲取運(yùn)行結(jié)果操作方法 
	def getResult(self):
		#將當(dāng)前界面中數(shù)字加入計(jì)算列表
		self.operationList.append(self.num.get())

		#開始計(jì)算 
		result = eval(''.join(self.operationList))
		self.num.set(result)

	#全部清空重新計(jì)算方法 
	def clearAll(self):
		#界面置0 計(jì)算列表置0
		self.num.set('0')
		self.operationList.clear()
		#運(yùn)算標(biāo)志復(fù)位
		self.isOper = False

	#實(shí)現(xiàn)退格鍵方法
	def backSpace(self):
		#獲取當(dāng)前顯示數(shù)字長度
		strLength = len(self.num.get())
		#如果當(dāng)前顯示有數(shù)字
		if strLength > 1:
			#刪除字串中最后一個(gè)字
			presentStr = self.num.get()
			presentStr = presentStr[:strLength - 1]
			self.num.set(presentStr)
		else:
			self.num.set('0')

	#正負(fù)號實(shí)現(xiàn)方法
	def pm(self):
		presentStr = self.num.get()
		#實(shí)現(xiàn)增加和去除負(fù)號
		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('微硬計(jì)算器')

		#生成用于保存數(shù)值的變量
		self.num = tkinter.StringVar()
		self.num.set(0)

		#運(yùn)算結(jié)果輸出位置
		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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonCE.bind('<Enter>',self.changeBg)
		buttonCE.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonC.bind('<Enter>',self.changeBg)
		buttonC.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonDel.bind('<Enter>',self.changeBg)
		buttonDel.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonDiv.bind('<Enter>',self.changeBg)
		buttonDiv.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button1.bind('<Enter>',self.changeBg)
		button1.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button2.bind('<Enter>',self.changeBg)
		button2.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button3.bind('<Enter>',self.changeBg)
		button3.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonX.bind('<Enter>',self.changeBg)
		buttonX.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button4.bind('<Enter>',self.changeBg)
		button4.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button5.bind('<Enter>',self.changeBg)
		button5.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button6.bind('<Enter>',self.changeBg)
		button6.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button_.bind('<Enter>',self.changeBg)
		button_.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button7.bind('<Enter>',self.changeBg)
		button7.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button8.bind('<Enter>',self.changeBg)
		button8.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button9.bind('<Enter>',self.changeBg)
		button9.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonAdd.bind('<Enter>',self.changeBg)
		buttonAdd.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonFlag.bind('<Enter>',self.changeBg)
		buttonFlag.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		button0.bind('<Enter>',self.changeBg)
		button0.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonPoint.bind('<Enter>',self.changeBg)
		buttonPoint.bind('<Leave>',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)
		#綁定按鈕 生成鼠標(biāo)經(jīng)過變色效果
		buttonEque.bind('<Enter>',self.changeBg)
		buttonEque.bind('<Leave>',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)

		#主窗口循環(huán)
		root.mainloop()


            
#實(shí)例化計(jì)算器對象
c = Calc()


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI