網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
效果如圖:
主要思路:
用列表保存按下的鍵,按下等于,轉(zhuǎn)換為字符串,利用內(nèi)置函數(shù)eval計(jì)算字符串的值。
代碼:
from tkinter import * ? W = 280 #窗口寬度 H = 460 #窗口高度 process_H = 110 #顯示運(yùn)算過(guò)程的標(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)算過(guò)程的標(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()
原文鏈接:https://blog.csdn.net/lanse_l/article/details/87175234
相關(guān)推薦
- 2022-05-31 Windows下搭建Redis集群的方法步驟_Redis
- 2022-07-17 C++深入講解new與deleted關(guān)鍵字的使用_C 語(yǔ)言
- 2022-11-28 詳解Pytorch如何利用yaml定義卷積網(wǎng)絡(luò)_python
- 2023-10-16 input框錄入身份證自動(dòng)填寫(xiě)性別年齡
- 2022-10-02 C#使用is、as關(guān)鍵字以及顯式強(qiáng)轉(zhuǎn)實(shí)現(xiàn)引用類(lèi)型轉(zhuǎn)換_C#教程
- 2022-05-14 解決Linux未啟用網(wǎng)卡的問(wèn)題_Linux
- 2023-01-15 React?Fiber樹(shù)的構(gòu)建和替換過(guò)程講解_React
- 2022-10-03 react實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽(tīng)方式_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支