新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么編寫計(jì)算器程序
該計(jì)算器是使用python tkinter模塊進(jìn)行開發(fā)的。下面給大家介紹具體代碼:

成都創(chuàng)新互聯(lián)公司溝通電話:028-86922220,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)公司網(wǎng)頁制作領(lǐng)域10多年,包括成都衛(wèi)生間隔斷等多個(gè)行業(yè)擁有多年建站經(jīng)驗(yàn),選擇成都創(chuàng)新互聯(lián)公司,為企業(yè)錦上添花。
效果如下圖:
代碼如下:
import tkinter #導(dǎo)入tkinter模塊
root = tkinter.Tk()
root.minsize(280,500)
root.title('李蛟龍的計(jì)算器')
#1.界面布局
#顯示面板
result = tkinter.StringVar()
result.set(0) #顯示面板顯示結(jié)果1,用于顯示默認(rèn)數(shù)字0
result2 = tkinter.StringVar() #顯示面板顯示結(jié)果2,用于顯示計(jì)算過程
result2.set('')
#顯示版
label = tkinter.Label(root,font = ('微軟雅黑',20),bg = '#EEE9E9',bd ='9',fg = '#828282',anchor = 'se',textvariable
= result2)
label.place(width = 280,height = 170)
label2 = tkinter.Label(root,font = ('微軟雅黑',30),bg = '#EEE9E9',bd ='9',fg = 'black',anchor = 'se',textvariable
= result)
label2.place(y = 170,width = 280,height = 60)
#數(shù)字鍵按鈕
btn7 = tkinter.Button(root,text = '7',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('7'))
btn7.place(x = 0,y = 285,width = 70,height = 55)
btn8 = tkinter.Button(root,text = '8',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('8'))
btn8.place(x = 70,y = 285,width = 70,height = 55)
btn9 = tkinter.Button(root,text = '9',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('9'))
btn9.place(x = 140,y = 285,width = 70,height = 55)
btn4 = tkinter.Button(root,text = '4',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('4'))
btn4.place(x = 0,y = 340,width = 70,height = 55)
btn5 = tkinter.Button(root,text = '5',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('5'))
btn5.place(x = 70,y = 340,width = 70,height = 55)
btn6 = tkinter.Button(root,text = '6',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('6'))
btn6.place(x = 140,y = 340,width = 70,height = 55)
btn1 = tkinter.Button(root,text = '1',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('1'))
btn1.place(x = 0,y = 395,width = 70,height = 55)
btn2 = tkinter.Button(root,text = '2',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('2'))
btn2.place(x = 70,y = 395,width = 70,height = 55)
btn3 = tkinter.Button(root,text = '3',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('3'))
btn3.place(x = 140,y = 395,width = 70,height = 55)
btn0 = tkinter.Button(root,text = '0',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :
pressNum('0'))
btn0.place(x = 70,y = 450,width = 70,height = 55)
#運(yùn)算符號(hào)按鈕
btnac = tkinter.Button(root,text = 'AC',bd = 0.5,font = ('黑體',20),fg = 'orange',command = lambda :pressCompute('AC'))
btnac.place(x = 0,y = 230,width = 70,height = 55)
btnback = tkinter.Button(root,text = '←',font = ('微軟雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:
pressCompute('b'))
btnback.place(x = 70,y = 230,width = 70,height = 55)
btndivi = tkinter.Button(root,text = '÷',font = ('微軟雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:
pressCompute('/'))
btndivi.place(x = 140,y = 230,width = 70,height = 55)
btnmul = tkinter.Button(root,text ='×',font = ('微軟雅黑',20),fg = "#4F4F4F",bd = 0.5,command = lambda:
pressCompute('*'))
btnmul.place(x = 210,y = 230,width = 70,height = 55)
btnsub = tkinter.Button(root,text = '-',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:
pressCompute('-'))
btnsub.place(x = 210,y = 285,width = 70,height = 55)
btnadd = tkinter.Button(root,text = '+',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:
pressCompute('+'))
btnadd.place(x = 210,y = 340,width = 70,height = 55)
btnequ = tkinter.Button(root,text = '=',bg = 'orange',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command =
lambda :pressEqual())
btnequ.place(x = 210,y = 395,width = 70,height = 110)
btnper = tkinter.Button(root,text = '%',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:
pressCompute('%'))
btnper.place(x = 0,y = 450,width = 70,height = 55)
btnpoint = tkinter.Button(root,text = '.',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:
pressCompute('.'))
btnpoint.place(x = 140,y = 450,width = 70,height = 55)
#操作函數(shù)
lists = [] #設(shè)置一個(gè)變量 保存運(yùn)算數(shù)字和符號(hào)的列表
isPressSign = False #添加一個(gè)判斷是否按下運(yùn)算符號(hào)的標(biāo)志,假設(shè)默認(rèn)沒有按下按鈕
isPressNum = False
#數(shù)字函數(shù)
def pressNum(num): #設(shè)置一個(gè)數(shù)字函數(shù) 判斷是否按下數(shù)字 并獲取數(shù)字將數(shù)字寫在顯示版上
global lists #全局化lists和按鈕狀態(tài)isPressSign
global isPressSign
if isPressSign == False:
pass
else: #重新將運(yùn)算符號(hào)狀態(tài)設(shè)置為否
result.set(0)
isPressSign = False
#判斷界面的數(shù)字是否為0
oldnum = result.get() #第一步
if oldnum =='0': #如過界面上數(shù)字為0 則獲取按下的數(shù)字
result.set(num)
else: #如果界面上的而數(shù)字不是0 則鏈接上新按下的數(shù)字
newnum = oldnum + num
result.set(newnum) #將按下的數(shù)字寫到面板中
#運(yùn)算函數(shù)
def pressCompute(sign):
global lists
global isPressSign
num = result.get() #獲取界面數(shù)字
lists.append(num) #保存界面獲取的數(shù)字到列表中
lists.append(sign) #講按下的運(yùn)算符號(hào)保存到列表中
isPressSign = True
if sign =='AC': #如果按下的是'AC'按鍵,則清空列表內(nèi)容,講屏幕上的數(shù)字鍵設(shè)置為默認(rèn)數(shù)字0
lists.clear()
result.set(0)
if sign =='b': #如果按下的是退格‘’,則選取當(dāng)前數(shù)字第一位到倒數(shù)第二位
a = num[0:-1]
lists.clear()
result.set(a)
#獲取運(yùn)算結(jié)果函數(shù)
def pressEqual():
global lists
global isPressSign
curnum = result.get() #設(shè)置當(dāng)前數(shù)字變量,并獲取添加到列表
lists.append(curnum)
computrStr = ''.join(lists) #講列表內(nèi)容用join命令將字符串鏈接起來
endNum = eval(computrStr) #用eval命令運(yùn)算字符串中的內(nèi)容
# a = str(endNum)
# b = '='+a #給運(yùn)算結(jié)果前添加一個(gè) ‘=’ 顯示 不過這樣寫會(huì)有BUG
不能連續(xù)運(yùn)算,這里注釋,不要 =
# c = b[0:10] #所有的運(yùn)算結(jié)果取9位數(shù)
result.set(endNum) #講運(yùn)算結(jié)果顯示到屏幕1
result2.set(computrStr) #將運(yùn)算過程顯示到屏幕2
lists.clear() #清空列表內(nèi)容
root.mainloop() 名稱欄目:創(chuàng)新互聯(lián)Python教程:python怎么編寫計(jì)算器程序
URL網(wǎng)址:http://www.fisionsoft.com.cn/article/ccdhsph.html


咨詢
建站咨詢
