欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 基于 tkinter 做個(gè)學(xué)生版的計(jì)算器

 更新時(shí)間:2021年09月18日 09:04:04   作者:顧木子吖  
這篇文章主要介紹了基于Python編寫(xiě)一個(gè)計(jì)算器程序,實(shí)現(xiàn)簡(jiǎn)單的加減乘除和取余二元運(yùn)算,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

導(dǎo)語(yǔ)

九月初家里的熊孩子終于開(kāi)始上學(xué)了!

半個(gè)月過(guò)去了,小孩子每周都會(huì)帶著一堆的數(shù)學(xué)作業(yè)回來(lái),哈哈哈哈~真好,在家做作業(yè)就沒(méi)時(shí)間打擾我寫(xiě)代碼了。

很贊,鵝鵝鵝餓鵝鵝鵝~曲項(xiàng)向天歌~~~~開(kāi)心到原地起飛。

孩子昨天回家之后吃完飯就悄咪咪的說(shuō),神神秘秘的我以為做什么?結(jié)果是班主任讓他們每個(gè)人帶一個(gè)計(jì)算器,平常做數(shù)學(xué)算數(shù)的時(shí)候可以在家用用,嗯哼~這還用賣(mài)嘛?

立馬給孩子用Python制作了一款簡(jiǎn)直一摸一樣的學(xué)生計(jì)算器~

正文

本文的學(xué)生計(jì)算器是基于tkinter做的界面化的小程序哈!

math模塊中定義了一些數(shù)學(xué)函數(shù)。由于這個(gè)模塊屬于編譯系統(tǒng)自帶,因此它可以被無(wú)條件調(diào)用。

都是自帶的所以不用安裝可以直接使用。

​定義各種運(yùn)算,設(shè)置顯示框字節(jié)等:

oot = tkinter.Tk()
root.resizable(width=False, height=False)
'''hypeparameter'''
# 是否按下了運(yùn)算符
IS_CALC = False
# 存儲(chǔ)數(shù)字
STORAGE = []
# 顯示框最多顯示多少個(gè)字符
MAXSHOWLEN = 18
# 當(dāng)前顯示的數(shù)字
CurrentShow = tkinter.StringVar()
CurrentShow.set('0')
 
 
'''按下數(shù)字鍵(0-9)'''
def pressNumber(number):
	global IS_CALC
	if IS_CALC:
		CurrentShow.set('0')
		IS_CALC = False
	if CurrentShow.get() == '0':
		CurrentShow.set(number)
	else:
		if len(CurrentShow.get()) < MAXSHOWLEN:
			CurrentShow.set(CurrentShow.get() + number)
 
 
'''按下小數(shù)點(diǎn)'''
def pressDP():
	global IS_CALC
	if IS_CALC:
		CurrentShow.set('0')
		IS_CALC = False
	if len(CurrentShow.get().split('.')) == 1:
		if len(CurrentShow.get()) < MAXSHOWLEN:
			CurrentShow.set(CurrentShow.get() + '.')
 
 
'''清零'''
def clearAll():
	global STORAGE
	global IS_CALC
	STORAGE.clear()
	IS_CALC = False
	CurrentShow.set('0')
 
 
'''清除當(dāng)前顯示框內(nèi)所有數(shù)字'''
def clearCurrent():
	CurrentShow.set('0')
 
 
'''刪除顯示框內(nèi)最后一個(gè)數(shù)字'''
def delOne():
	global IS_CALC
	if IS_CALC:
		CurrentShow.set('0')
		IS_CALC = False
	if CurrentShow.get() != '0':
		if len(CurrentShow.get()) > 1:
			CurrentShow.set(CurrentShow.get()[:-1])
		else:
			CurrentShow.set('0')
 
 
'''計(jì)算答案修正'''
def modifyResult(result):
	result = str(result)
	if len(result) > MAXSHOWLEN:
		if len(result.split('.')[0]) > MAXSHOWLEN:
			result = 'Overflow'
		else:
			# 直接舍去不考慮四舍五入問(wèn)題
			result = result[:MAXSHOWLEN]
	return result

按下運(yùn)算符:

def pressOperator(operator):
	global STORAGE
	global IS_CALC
	if operator == '+/-':
		if CurrentShow.get().startswith('-'):
			CurrentShow.set(CurrentShow.get()[1:])
		else:
			CurrentShow.set('-'+CurrentShow.get())
	elif operator == '1/x':
		try:
			result = 1 / float(CurrentShow.get())
		except:
			result = 'illegal operation'
		result = modifyResult(result)
		CurrentShow.set(result)
		IS_CALC = True
	elif operator == 'sqrt':
		try:
			result = math.sqrt(float(CurrentShow.get()))
		except:
			result = 'illegal operation'
		result = modifyResult(result)
		CurrentShow.set(result)
		IS_CALC = True
	elif operator == 'MC':
		STORAGE.clear()
	elif operator == 'MR':
		if IS_CALC:
			CurrentShow.set('0')
		STORAGE.append(CurrentShow.get())
		expression = ''.join(STORAGE)
		try:
			result = eval(expression)
		except:
			result = 'illegal operation'
		result = modifyResult(result)
		CurrentShow.set(result)
		IS_CALC = True
	elif operator == 'MS':
		STORAGE.clear()
		STORAGE.append(CurrentShow.get())
	elif operator == 'M+':
		STORAGE.append(CurrentShow.get())
	elif operator == 'M-':
		if CurrentShow.get().startswith('-'):
			STORAGE.append(CurrentShow.get())
		else:
			STORAGE.append('-' + CurrentShow.get())
	elif operator in ['+', '-', '*', '/', '%']:
		STORAGE.append(CurrentShow.get())
		STORAGE.append(operator)
		IS_CALC = True
	elif operator == '=':
		if IS_CALC:
			CurrentShow.set('0')
		STORAGE.append(CurrentShow.get())
		expression = ''.join(STORAGE)
		try:
			result = eval(expression)
		# 除以0的情況
		except:
			result = 'illegal operation'
		result = modifyResult(result)
		CurrentShow.set(result)
		STORAGE.clear()
		IS_CALC = True

學(xué)生計(jì)算器的文本布局界面:

def Demo():
	root.minsize(320, 420)
	root.title('學(xué)生計(jì)算器')
	# 布局
	# --文本框
	label = tkinter.Label(root, textvariable=CurrentShow, bg='black', anchor='e', bd=5, fg='white', font=('楷體', 20))
	label.place(x=20, y=50, width=280, height=50)
	# --第一行
	# ----Memory clear
	button1_1 = tkinter.Button(text='MC', bg='#666', bd=2, command=lambda:pressOperator('MC'))
	button1_1.place(x=20, y=110, width=50, height=35)
	# ----Memory read
	button1_2 = tkinter.Button(text='MR', bg='#666', bd=2, command=lambda:pressOperator('MR'))
	button1_2.place(x=77.5, y=110, width=50, height=35)
	# ----Memory save
	button1_3 = tkinter.Button(text='MS', bg='#666', bd=2, command=lambda:pressOperator('MS'))
	button1_3.place(x=135, y=110, width=50, height=35)
	# ----Memory +
	button1_4 = tkinter.Button(text='M+', bg='#666', bd=2, command=lambda:pressOperator('M+'))
	button1_4.place(x=192.5, y=110, width=50, height=35)
	# ----Memory -
	button1_5 = tkinter.Button(text='M-', bg='#666', bd=2, command=lambda:pressOperator('M-'))
	button1_5.place(x=250, y=110, width=50, height=35)
	# --第二行
	# ----刪除單個(gè)數(shù)字
	button2_1 = tkinter.Button(text='del', bg='#666', bd=2, command=lambda:delOne())
	button2_1.place(x=20, y=155, width=50, height=35)
	# ----清除當(dāng)前顯示框內(nèi)所有數(shù)字
	button2_2 = tkinter.Button(text='CE', bg='#666', bd=2, command=lambda:clearCurrent())
	button2_2.place(x=77.5, y=155, width=50, height=35)
	# ----清零(相當(dāng)于重啟)
	button2_3 = tkinter.Button(text='C', bg='#666', bd=2, command=lambda:clearAll())
	button2_3.place(x=135, y=155, width=50, height=35)
	# ----取反
	button2_4 = tkinter.Button(text='+/-', bg='#666', bd=2, command=lambda:pressOperator('+/-'))
	button2_4.place(x=192.5, y=155, width=50, height=35)
	# ----開(kāi)根號(hào)
	button2_5 = tkinter.Button(text='sqrt', bg='#666', bd=2, command=lambda:pressOperator('sqrt'))
	button2_5.place(x=250, y=155, width=50, height=35)
	# --第三行
	# ----7
	button3_1 = tkinter.Button(text='7', bg='#bbbbbb', bd=2, command=lambda:pressNumber('7'))
	button3_1.place(x=20, y=200, width=50, height=35)
	# ----8
	button3_2 = tkinter.Button(text='8', bg='#bbbbbb', bd=2, command=lambda:pressNumber('8'))
	button3_2.place(x=77.5, y=200, width=50, height=35)
	# ----9
	button3_3 = tkinter.Button(text='9', bg='#bbbbbb', bd=2, command=lambda:pressNumber('9'))
	button3_3.place(x=135, y=200, width=50, height=35)
	# ----除
	button3_4 = tkinter.Button(text='/', bg='#708069', bd=2, command=lambda:pressOperator('/'))
	button3_4.place(x=192.5, y=200, width=50, height=35)
	# ----取余
	button3_5 = tkinter.Button(text='%', bg='#708069', bd=2, command=lambda:pressOperator('%'))
	button3_5.place(x=250, y=200, width=50, height=35)
	# --第四行
	# ----4
	button4_1 = tkinter.Button(text='4', bg='#bbbbbb', bd=2, command=lambda:pressNumber('4'))
	button4_1.place(x=20, y=245, width=50, height=35)
	# ----5
	button4_2 = tkinter.Button(text='5', bg='#bbbbbb', bd=2, command=lambda:pressNumber('5'))
	button4_2.place(x=77.5, y=245, width=50, height=35)
	# ----6
	button4_3 = tkinter.Button(text='6', bg='#bbbbbb', bd=2, command=lambda:pressNumber('6'))
	button4_3.place(x=135, y=245, width=50, height=35)
	# ----乘
	button4_4 = tkinter.Button(text='*', bg='#708069', bd=2, command=lambda:pressOperator('*'))
	button4_4.place(x=192.5, y=245, width=50, height=35)
	# ----取導(dǎo)數(shù)
	button4_5 = tkinter.Button(text='1/x', bg='#708069', bd=2, command=lambda:pressOperator('1/x'))
	button4_5.place(x=250, y=245, width=50, height=35)
	# --第五行
	# ----3
	button5_1 = tkinter.Button(text='3', bg='#bbbbbb', bd=2, command=lambda:pressNumber('3'))
	button5_1.place(x=20, y=290, width=50, height=35)
	# ----2
	button5_2 = tkinter.Button(text='2', bg='#bbbbbb', bd=2, command=lambda:pressNumber('2'))
	button5_2.place(x=77.5, y=290, width=50, height=35)
	# ----1
	button5_3 = tkinter.Button(text='1', bg='#bbbbbb', bd=2, command=lambda:pressNumber('1'))
	button5_3.place(x=135, y=290, width=50, height=35)
	# ----減
	button5_4 = tkinter.Button(text='-', bg='#708069', bd=2, command=lambda:pressOperator('-'))
	button5_4.place(x=192.5, y=290, width=50, height=35)
	# ----等于
	button5_5 = tkinter.Button(text='=', bg='#708069', bd=2, command=lambda:pressOperator('='))
	button5_5.place(x=250, y=290, width=50, height=80)
	# --第六行
	# ----0
	button6_1 = tkinter.Button(text='0', bg='#bbbbbb', bd=2, command=lambda:pressNumber('0'))
	button6_1.place(x=20, y=335, width=107.5, height=35)
	# ----小數(shù)點(diǎn)
	button6_2 = tkinter.Button(text='.', bg='#bbbbbb', bd=2, command=lambda:pressDP())
	button6_2.place(x=135, y=335, width=50, height=35)
	# ----加
	button6_3 = tkinter.Button(text='+', bg='#708069', bd=2, command=lambda:pressOperator('+'))
	button6_3.place(x=192.5, y=335, width=50, height=35)
	root.mainloop()

效果如下:

​​

​​​​

總結(jié)

好啦!學(xué)生計(jì)算器就寫(xiě)完啦,簡(jiǎn)單不~記得三連哦,嘿嘿。

到此這篇關(guān)于python 基于 tkinter 做個(gè)學(xué)生版的計(jì)算器的文章就介紹到這了,更多相關(guān)python tkinter 計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度

    Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度

    這篇文章主要介紹了Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • PyCharm提示No Python Interpreter的正確解決辦法

    PyCharm提示No Python Interpreter的正確解決辦法

    剛學(xué)Python時(shí),拿到一個(gè)Python項(xiàng)目,想用pycharm打開(kāi)運(yùn)行卻報(bào)錯(cuò)了,這篇文章主要給大家介紹了關(guān)于PyCharm提示No Python Interpreter的正確解決辦法,需要的朋友可以參考下
    2023-10-10
  • Python撲克牌21點(diǎn)游戲?qū)嵗a

    Python撲克牌21點(diǎn)游戲?qū)嵗a

    大家好,本篇文章主要講的是Python撲克牌21點(diǎn)游戲?qū)嵗a,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽
    2021-12-12
  • tensorflow dataset.shuffle、dataset.batch、dataset.repeat順序區(qū)別詳解

    tensorflow dataset.shuffle、dataset.batch、dataset.repeat順序區(qū)別詳

    這篇文章主要介紹了tensorflow dataset.shuffle、dataset.batch、dataset.repeat順序區(qū)別詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • jupyter關(guān)于pandas的dataframe行列顯示不全與復(fù)原問(wèn)題

    jupyter關(guān)于pandas的dataframe行列顯示不全與復(fù)原問(wèn)題

    這篇文章主要介紹了jupyter關(guān)于pandas的dataframe行列顯示不全與復(fù)原問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python虛擬環(huán)境庫(kù)virtualenvwrapper安裝及使用

    Python虛擬環(huán)境庫(kù)virtualenvwrapper安裝及使用

    這篇文章主要介紹了Python虛擬環(huán)境庫(kù)virtualenvwrapper安裝及使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 使用Python3 poplib模塊刪除服務(wù)器多天前的郵件實(shí)現(xiàn)代碼

    使用Python3 poplib模塊刪除服務(wù)器多天前的郵件實(shí)現(xiàn)代碼

    這篇文章主要介紹了使用Python3 poplib模塊刪除多天前的郵件的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Python Matplotlib初階使用入門(mén)教程

    Python Matplotlib初階使用入門(mén)教程

    本文介紹Python Matplotlib庫(kù)的入門(mén)求生級(jí)使用方法,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • Python實(shí)現(xiàn)的列表排序、反轉(zhuǎn)操作示例

    Python實(shí)現(xiàn)的列表排序、反轉(zhuǎn)操作示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的列表排序、反轉(zhuǎn)操作,結(jié)合實(shí)例形式分析了Python針對(duì)列表的sort排序、以及基于reverse、切片的反轉(zhuǎn)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • Python Pandas基礎(chǔ)操作詳解

    Python Pandas基礎(chǔ)操作詳解

    這篇文章主要介紹了Python使用Pandas庫(kù)常見(jiàn)操作,結(jié)合實(shí)例形式詳細(xì)分析了Python Pandas模塊的功能、原理、數(shù)據(jù)對(duì)象創(chuàng)建、查看、選擇等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2021-10-10

最新評(píng)論