網站首頁 編程語言 正文
基于tkinter圖形化編程的簡易計算器,供大家參考,具體內容如下
代碼如下:
import re
import tkinter
import tkinter.messagebox
root = tkinter.Tk()
# 設置窗口大小和位置
root.geometry('300x270+400+100')
# 不允許改變窗口大小
root.resizable(False, False)
# 設置窗口標題
root.title('簡易計算機'.center(25))
# 設置用來顯示信息的文本框,并設置為只讀
contentVar = tkinter.StringVar(root, '0')
contentEntry = tkinter.Entry(root, textvariable=contentVar)
contentEntry['state'] = 'readonly'
contentEntry.place(x=10, y=10, width=280, height=20)
# 按鈕通用代碼
def buttonClick(btn):
? ? content = contentVar.get()
? ? # 如果已有內容是以小數點開頭的,前面加0
? ? if content.startswith('.'):
? ? ? ? content = '0' + content
? ? # 根據不同按鈕作出相應的處理
? ? if btn in '0123456789':
? ? ? ? if content == '0':
? ? ? ? ? ? content = ''
? ? ? ? content += btn
? ? elif btn == '.':
? ? ? ? lastPart = re.split(r'\+|-|\*|/', content)[-1]
? ? ? ? if '.' in lastPart:
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '小數點太多了')
? ? ? ? ? ? return
? ? ? ? else:
? ? ? ? ? ? content += btn
? ? elif btn == 'C':
? ? ? ? content = '0'
? ? elif btn == '=':
? ? ? ? try:
? ? ? ? ? ? # 對輸入的表達式求值
? ? ? ? ? ? content = str(eval(content))
? ? ? ? except:
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '表達式錯誤')
? ? ? ? ? ? return
? ? elif btn in operators or btn in operators1:
? ? ? ? if content.endswith(operators) or content.endswith(operators1):
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '不允許存在連續運算符')
? ? ? ? ? ? return
? ? ? ? content += btn
? ? elif btn == 'Sqrt':
? ? ? ? n = content.split('.')
? ? ? ? if all(map(lambda x: x.isdigit(), n)):
? ? ? ? ? ? content = eval(content) ** 0.5
? ? ? ? else:
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '表達式錯誤')
? ? ? ? ? ? return
? ? contentVar.set(content)
# 放置Clear按鈕和“=”按鈕
btnClear = tkinter.Button(root, text='C', command=lambda: buttonClick('C'))
btnClear.place(x=20, y=40, width=50, height=20)
btnCompute = tkinter.Button(root, text='=', command=lambda: buttonClick('='))
btnCompute.place(x=230, y=230, width=50, height=20)
# 放置10個數字、小數點和計算平方根的按鈕
digits = list('7894561230.') + ['Sqrt']
index = 0
for row in range(4):
? ? for col in range(3):
? ? ? ? d = digits[index]
? ? ? ? index += 1
? ? ? ? btnDigit = tkinter.Button(root, text=d, command=lambda x=d: buttonClick(x))
? ? ? ? btnDigit.place(x=20 + col * 70, y=80 + row * 50, width=50, height=20)
# 放置運算符按鈕
index = 0
operators1 = ('/', '**', '//')
for index, operator1 in enumerate(operators1):
? ? btnOperator = tkinter.Button(root, text=operator1, command=lambda x=operator1: buttonClick(x))
? ? btnOperator.place(x=230, y=80 + index * 50, width=50, height=20)
operators = ('+', '-', '*')
for index, operator in enumerate(operators):
? ? if operator == '+' or operator == '-' or operator == '*':
? ? ? ? btnOperator = tkinter.Button(root, text=operator, command=lambda x=operator: buttonClick(x))
? ? ? ? btnOperator.place(x=90 + index * 70, y=40, width=50, height=20)
root.mainloop()
代碼運行結果(簡易計算機):
重點
1、選擇需要使用的圖形界面(窗口,文本框,按鈕);
2、圖形界面參數的設置(位置、大小、初始化參數等);
3、重復運算符或小數點報錯(當然你也可以編寫程序直接不報錯但不寫入重復運算符或小數點);
4、運算結果的實現,使用這個簡易計算器,實際上其實就是先輸入一串字符串表達式,然后使用eval() 函數用來執行該字符串表達式,并返回表達式的值,該值就是我們要計算的結果。
原文鏈接:https://blog.csdn.net/qq_45440931/article/details/121503881
相關推薦
- 2022-04-20 Docker?Compose部署Nginx的方法步驟_docker
- 2022-08-25 C語言詳細分析宏定義與預處理命令的應用_C 語言
- 2022-01-13 element ui 表格雙擊單元格修改
- 2022-01-19 el-table 定位 跳轉 至指定 行 位置,保證數據更新后位置不變
- 2022-09-02 C#中ftp檢測目錄是否存在和創建文件夾的實現_C#教程
- 2022-12-19 Oracle?Instr函數實例講解_oracle
- 2022-05-01 pytorch中的torch.nn.Conv2d()函數圖文詳解_python
- 2022-04-08 pytorch?plt.savefig()的用法及保存路徑_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支