最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

導(dǎo)語

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

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

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

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

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

正文

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

math模塊中定義了一些數(shù)學(xué)函數(shù)。由于這個(gè)模塊屬于編譯系統(tǒng)自帶,因此它可以被無條件調(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:
			# 直接舍去不考慮四舍五入問題
			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)
	# ----開根號(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ì)算器就寫完啦,簡(jiǎn)單不~記得三連哦,嘿嘿。

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

相關(guān)文章

  • django日志默認(rèn)打印request請(qǐng)求信息的方法示例

    django日志默認(rèn)打印request請(qǐng)求信息的方法示例

    這篇文章主要給大家介紹了關(guān)于django日志默認(rèn)打印request請(qǐng)求信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • python實(shí)現(xiàn)轉(zhuǎn)盤效果 python實(shí)現(xiàn)輪盤抽獎(jiǎng)游戲

    python實(shí)現(xiàn)轉(zhuǎn)盤效果 python實(shí)現(xiàn)輪盤抽獎(jiǎng)游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)轉(zhuǎn)盤效果,python實(shí)現(xiàn)輪盤抽獎(jiǎng)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Python中request庫的各種用法詳細(xì)解析

    Python中request庫的各種用法詳細(xì)解析

    本文詳細(xì)介紹了Python的requests庫的安裝與使用,包括HTTP請(qǐng)求方法、請(qǐng)求頭、請(qǐng)求體的基本概念,以及發(fā)送GET和POST請(qǐng)求的基本用法,同時(shí),探討了會(huì)話對(duì)象、處理重定向、超時(shí)設(shè)置、代理支持等高級(jí)功能,幫助讀者更高效地處理復(fù)雜的HTTP請(qǐng)求場(chǎng)景,需要的朋友可以參考下
    2024-10-10
  • Python網(wǎng)絡(luò)編程詳解(常用庫、代碼案例、環(huán)境搭建等)

    Python網(wǎng)絡(luò)編程詳解(常用庫、代碼案例、環(huán)境搭建等)

    網(wǎng)絡(luò)編程是Python中非常重要的一個(gè)領(lǐng)域,涉及到的常用庫包括socket、asyncio、http、requests、websockets等,下面我們將從常用庫、庫的詳細(xì)用法、完整代碼案例、依賴項(xiàng)、環(huán)境搭建、注意事項(xiàng)和常見問題等方面,對(duì)Python網(wǎng)絡(luò)編程進(jìn)行詳細(xì)講解,需要的朋友可以參考下
    2025-03-03
  • python自動(dòng)化之Ansible的安裝教程

    python自動(dòng)化之Ansible的安裝教程

    這篇文章主要介紹了python自動(dòng)化之Ansible的安裝方法,結(jié)合實(shí)例形式分析了自動(dòng)化運(yùn)維工具Ansible的安裝步驟及相關(guān)操作命令,需要的朋友可以參考下
    2019-06-06
  • Python數(shù)據(jù)可視化實(shí)現(xiàn)正態(tài)分布(高斯分布)

    Python數(shù)據(jù)可視化實(shí)現(xiàn)正態(tài)分布(高斯分布)

    這篇文章主要介紹了Python數(shù)據(jù)可視化實(shí)現(xiàn)正態(tài)分布(高斯分布),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python小練習(xí)之爬魷魚游戲的評(píng)價(jià)生成詞云

    python小練習(xí)之爬魷魚游戲的評(píng)價(jià)生成詞云

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python爬取熱火的魷魚游戲評(píng)價(jià),大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-10-10
  • Django 外鍵查詢的實(shí)現(xiàn)

    Django 外鍵查詢的實(shí)現(xiàn)

    今天學(xué)習(xí)了一下外鍵查詢的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • python pyqtgraph 保存圖片到本地的實(shí)例

    python pyqtgraph 保存圖片到本地的實(shí)例

    這篇文章主要介紹了python pyqtgraph 保存圖片到本地的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • pytorch關(guān)于卷積操作的初始化方式(kaiming_uniform_詳解)

    pytorch關(guān)于卷積操作的初始化方式(kaiming_uniform_詳解)

    這篇文章主要介紹了pytorch關(guān)于卷積操作的初始化方式(kaiming_uniform_詳解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評(píng)論

宜宾县| 东源县| 崇文区| 平邑县| 许昌县| 长岭县| 巴青县| 兴城市| 南宁市| 永仁县| 龙井市| 长宁区| 淅川县| 刚察县| 晋江市| 汝城县| 思南县| 九龙县| 海林市| 辛集市| 广西| 梁河县| 江都市| 乌审旗| 乌什县| 临沧市| 邓州市| 于田县| 松溪县| 越西县| 张家口市| 兰坪| 雅安市| 西乌珠穆沁旗| 宜兰县| 闽侯县| 海晏县| 凉城县| 罗源县| 阳西县| 班戈县|