python 基于 tkinter 做個(gè)學(xué)生版的計(jì)算器
導(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)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信