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

python制作簡單計(jì)算器功能

 更新時(shí)間:2022年02月07日 09:42:41   作者:丶藍(lán)色  
這篇文章主要為大家詳細(xì)介紹了python制作簡單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)簡單計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下

效果如圖:

主要思路:

用列表保存按下的鍵,按下等于,轉(zhuǎn)換為字符串,利用內(nèi)置函數(shù)eval計(jì)算字符串的值。

代碼:

from tkinter import *
?
W = 280 #窗口寬度
H = 460 #窗口高度
process_H = 110 #顯示運(yùn)算過程的標(biāo)簽高度
result_H = 50 ? #顯示運(yùn)算結(jié)果的標(biāo)簽高度
msFont = '微軟雅黑' #字體
fontSize = 20 #字體大小
?
btnBoderWidth = 0.5 #邊框?qū)挾?
btnColor = '#4F4F4F' #按鈕顏色
btnWidth = 70 #按鈕寬度
btnHeight = 60 #按鈕高度
?
mainWindows = Tk()
mainWindows.title('計(jì)算器')
mainWindows.minsize(W,H)
?
str_process = StringVar()
str_process.set("")
str_result = StringVar()
str_result.set("0")
?
process = Label(mainWindows,font=(msFont,fontSize),bg='orange',anchor='se',wraplength='280',textvariable=str_process)
process.place(width=W,height=process_H) #顯示運(yùn)算過程的標(biāo)簽
result = Label(mainWindows,font=(msFont,fontSize+10),bg='orange',anchor='se',textvariable=str_result)
result.place(y=process_H,width=W,height=result_H) #顯示運(yùn)算結(jié)果的標(biāo)簽
?
button_AC = Button(mainWindows,font=(msFont,fontSize),text='AC',fg='orange',bd=btnBoderWidth,command=lambda :clickAC())
button_AC.place(x=0,y=process_H+result_H,width=btnWidth,height=btnHeight)
button_back = Button(mainWindows,font=(msFont,fontSize),text='←',fg=btnColor,bd=btnBoderWidth,command=lambda :clickBack())
button_back.place(x=btnWidth,y=process_H+result_H,width=btnWidth,height=btnHeight)
button_div = Button(mainWindows,font=(msFont,fontSize),text='÷',fg=btnColor,bd=btnBoderWidth,command=lambda :clickOper('/'))
button_div.place(x=btnWidth*2,y=process_H+result_H,width=btnWidth,height=btnHeight)
button_mul = Button(mainWindows,font=(msFont,fontSize),text='×',fg=btnColor,bd=btnBoderWidth,command=lambda :clickOper('*'))
button_mul.place(x=btnWidth*3,y=process_H+result_H,width=btnWidth,height=btnHeight)
?
button_7 = Button(mainWindows,font=(msFont,fontSize),text='7',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('7'))
button_7.place(x=0,y=process_H+result_H+btnHeight,width=btnWidth,height=btnHeight)
button_8 = Button(mainWindows,font=(msFont,fontSize),text='8',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('8'))
button_8.place(x=btnWidth,y=process_H+result_H+btnHeight,width=btnWidth,height=btnHeight)
button_9 = Button(mainWindows,font=(msFont,fontSize),text='9',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('9'))
button_9.place(x=btnWidth*2,y=process_H+result_H+btnHeight,width=btnWidth,height=btnHeight)
button_minus = Button(mainWindows,font=(msFont,fontSize),text='-',fg=btnColor,bd=btnBoderWidth,command=lambda :clickOper('-'))
button_minus.place(x=btnWidth*3,y=process_H+result_H+btnHeight,width=btnWidth,height=btnHeight)
?
button_4 = Button(mainWindows,font=(msFont,fontSize),text='4',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('4'))
button_4.place(x=0,y=process_H+result_H+btnHeight*2,width=btnWidth,height=btnHeight)
button_5 = Button(mainWindows,font=(msFont,fontSize),text='5',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('5'))
button_5.place(x=btnWidth,y=process_H+result_H+btnHeight*2,width=btnWidth,height=btnHeight)
button_6 = Button(mainWindows,font=(msFont,fontSize),text='6',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('6'))
button_6.place(x=btnWidth*2,y=process_H+result_H+btnHeight*2,width=btnWidth,height=btnHeight)
button_add = Button(mainWindows,font=(msFont,fontSize),text='+',fg=btnColor,bd=btnBoderWidth,command=lambda :clickOper('+'))
button_add.place(x=btnWidth*3,y=process_H+result_H+btnHeight*2,width=btnWidth,height=btnHeight)
?
button_1 = Button(mainWindows,font=(msFont,fontSize),text='1',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('1'))
button_1.place(x=0,y=process_H+result_H+btnHeight*3,width=btnWidth,height=btnHeight)
button_2 = Button(mainWindows,font=(msFont,fontSize),text='2',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('2'))
button_2.place(x=btnWidth,y=process_H+result_H+btnHeight*3,width=btnWidth,height=btnHeight)
button_3 = Button(mainWindows,font=(msFont,fontSize),text='3',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('3'))
button_3.place(x=btnWidth*2,y=process_H+result_H+btnHeight*3,width=btnWidth,height=btnHeight)
button_equal = Button(mainWindows,font=(msFont,fontSize),text='=',bg='orange',fg=btnColor,bd=btnBoderWidth,command=lambda :clickEqual())
button_equal.place(x=btnWidth*3,y=process_H+result_H+btnHeight*3,width=btnWidth,height=btnHeight*2)
?
button_percent = Button(mainWindows,font=(msFont,fontSize),text='%',fg=btnColor,bd=btnBoderWidth,command=lambda :clickOper("%"))
button_percent.place(x=0,y=process_H+result_H+btnHeight*4,width=btnWidth,height=btnHeight)
button_0 = Button(mainWindows,font=(msFont,fontSize),text='0',fg=btnColor,bd=btnBoderWidth,command=lambda :clickNum('0'))
button_0.place(x=btnWidth,y=process_H+result_H+btnHeight*4,width=btnWidth,height=btnHeight)
button_point = Button(mainWindows,font=(msFont,fontSize),text='.',fg=btnColor,bd=btnBoderWidth,command=lambda :clickPoint())
button_point.place(x=btnWidth*2,y=process_H+result_H+btnHeight*4,width=btnWidth,height=btnHeight)
?
process_list = []
s_result = ""
isNum = [False] #上一位按下的是否是數(shù)字
point = [True] #小數(shù)點(diǎn)使用情況
def clickNum(num): ?#按下數(shù)字
? ? isNum.append(True)
? ? point.append(point[-1]) #按下數(shù)字,小數(shù)點(diǎn)的標(biāo)志不變
? ? process_list.append(num)
? ? s_process = "".join(process_list)
? ? str_process.set(s_process)
?
def clickOper(sign):#按下運(yùn)算符
? ? global isNum,point
? ? if isNum[-1]:
? ? ? ? process_list.append(sign)
? ? ? ? isNum.append(False)
? ? ? ? point.append(True) #按下運(yùn)算符,小數(shù)點(diǎn)標(biāo)志為可以按下小數(shù)點(diǎn)
? ? else:
? ? ? ? process_list.pop()
? ? ? ? process_list.append(sign)
? ? s_process = "".join(process_list)
? ? str_process.set(s_process)
?
def clickEqual():#按下等于
? ? global s_result
? ? s_process = "".join(process_list)
? ? s_result = eval(s_process)
? ? s_result = str(s_result)[0:11] #結(jié)果只顯示11位
? ? str_process.set(s_process)
? ? str_result.set(s_result)
?
def clickAC():#按下清除
? ? global s_result,isNum,point
? ? s_result = "0"
? ? isNum = [False] ?#狀態(tài)回到初始時(shí)候
? ? point = [True]
? ? process_list.clear()
? ? str_result.set(s_result)
? ? str_process.set("")
def clickBack(): #按下退格鍵
? ? global point, isNum
? ? if len(process_list) > 0:
? ? ? ? isNum.pop(-1) #刪除最后一位的狀態(tài)
? ? ? ? point.pop(-1)
? ? ? ? process_list.pop()
? ? ? ? s_process = "".join(process_list)
? ? ? ? str_process.set(s_process)
?
def clickPoint():#按下小數(shù)點(diǎn)
? ? global point,isNum
? ? if isNum[-1] and point[-1]:
? ? ? ? process_list.append(".")
? ? ? ? s_process = "".join(process_list)
? ? ? ? str_process.set(s_process)
? ? ? ? isNum.append(False)
? ? ? ? point.append(False)
?
mainWindows.mainloop()

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 簡單介紹一下pyinstaller打包以及安全性的實(shí)現(xiàn)

    簡單介紹一下pyinstaller打包以及安全性的實(shí)現(xiàn)

    這篇文章主要介紹了簡單介紹一下pyinstaller打包以及安全性的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Python縮進(jìn)和冒號(hào)詳解

    Python縮進(jìn)和冒號(hào)詳解

    下面小編就為大家?guī)硪黄狿ython縮進(jìn)和冒號(hào)詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • Python中圖像通用操作的實(shí)現(xiàn)代碼

    Python中圖像通用操作的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Python中圖像通用操作的實(shí)現(xiàn),例如:圖像旋轉(zhuǎn)、圖像縮放等,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2023-07-07
  • Python開發(fā)常用五種循環(huán)方式的場景性能比較

    Python開發(fā)常用五種循環(huán)方式的場景性能比較

    Python是一門高級(jí)編程語言,其擁有多種循環(huán)方式,如for循環(huán)、while循環(huán)、do-while循環(huán)等。本文將逐個(gè)分析Python所有的循環(huán)執(zhí)行效率和適用場景,需要的可以參考一下
    2023-04-04
  • Python實(shí)現(xiàn)將橫表和縱表任意轉(zhuǎn)換的兩種方法

    Python實(shí)現(xiàn)將橫表和縱表任意轉(zhuǎn)換的兩種方法

    在日常做數(shù)據(jù)分析,接收到最多的表格是縱表,每個(gè)字段變量都有很長數(shù)據(jù)的長表,我們稱之為縱向數(shù)據(jù),但是,有時(shí)候,我們也會(huì)遇到橫表,對(duì)于橫向數(shù)據(jù),我們會(huì)數(shù)據(jù)轉(zhuǎn)化,將其轉(zhuǎn)化為縱向數(shù)據(jù),感興趣的同學(xué)跟著小編一起來學(xué)習(xí)吧
    2023-12-12
  • CentOS安裝OpenSSL1.1.1全過程

    CentOS安裝OpenSSL1.1.1全過程

    文章介紹了從頭開始編譯安裝Python3.10的步驟,包括檢查和安裝必要的依賴項(xiàng)、下載并解壓源碼、配置和編譯環(huán)境、創(chuàng)建軟連接以及配置環(huán)境變量,最后驗(yàn)證安裝是否成功
    2025-03-03
  • Python unittest如何生成HTMLTestRunner模塊

    Python unittest如何生成HTMLTestRunner模塊

    這篇文章主要介紹了Python unittest如何生成HTMLTestRunner模塊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Python腳本實(shí)現(xiàn)調(diào)用手機(jī)攝像頭

    Python腳本實(shí)現(xiàn)調(diào)用手機(jī)攝像頭

    這篇文章主要為大家詳細(xì)介紹了Python如何通過,腳本實(shí)現(xiàn)調(diào)用手機(jī)攝像頭,這樣就能隨時(shí)隨地用電腦偷偷看看男朋友都在干啥了,感興趣的小伙伴可以了解下
    2025-03-03
  • 利用keras加載訓(xùn)練好的.H5文件,并實(shí)現(xiàn)預(yù)測圖片

    利用keras加載訓(xùn)練好的.H5文件,并實(shí)現(xiàn)預(yù)測圖片

    今天小編就為大家分享一篇利用keras加載訓(xùn)練好的.H5文件,并實(shí)現(xiàn)預(yù)測圖片,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • 給Python中的MySQLdb模塊添加超時(shí)功能的教程

    給Python中的MySQLdb模塊添加超時(shí)功能的教程

    這篇文章主要介紹了給Python中的MySQLdb模塊添加超時(shí)功能的教程,timeout功能在服務(wù)器的運(yùn)維當(dāng)中非常有用,需要的朋友可以參考下
    2015-05-05

最新評(píng)論

额尔古纳市| 涞水县| 汽车| 汤阴县| 射阳县| 栾川县| 郧西县| 莱芜市| 甘肃省| 永春县| 汽车| 红安县| 泰来县| 台北县| 平湖市| 华容县| 明光市| 正蓝旗| 井研县| 长丰县| 东兰县| 长泰县| 江西省| 大田县| 北海市| 班玛县| 子长县| 枝江市| 邹城市| 金阳县| 德格县| 迁安市| 白银市| 聂荣县| 长白| 肃北| 海丰县| 南丹县| 大城县| 同仁县| 孟津县|