python實現(xiàn)簡單的計算器功能
本文實例為大家分享了python實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下
今天學(xué)習(xí)到python中界面設(shè)計部分,常用的幾種圖形化界面庫有:Jython、wxPython和tkinter。
主要介紹tkinter模塊,tkinter模塊(tk接口)是Python的標(biāo)準(zhǔn)tk GUI工具包的接口。tk和tkinter可以在大多數(shù)的UNIX平臺下使用,同樣可以應(yīng)用在Windows和Macintosh系統(tǒng)里。Tk8.0的后續(xù)版本可以實現(xiàn)本地窗口風(fēng)格,并良好地運行在絕大多數(shù)平臺中。
下面使用tkinter設(shè)計完成計算器功能。
(1)首先呈現(xiàn)一下計算器初始界面:

(2)簡單說明:已經(jīng)實現(xiàn)計算器的基本功能
(3)主要代碼說明:
①導(dǎo)入包
import tkinter from tkinter import * import re import tkinter.messagebox
②界面布局設(shè)置
# 創(chuàng)建主窗口
root = Tk()
# 設(shè)置窗口大小和位置
root.title("---計算器---")
root.geometry("320x210+500+200")
# 自動刷新字符串變量,可用 set 和 get 方法進行傳值和取值
contentVar = tkinter.StringVar(root,'')
# 創(chuàng)建單行文本框
contentEntry = tkinter.Entry(root, textvariable=contentVar)
# 設(shè)置文本框坐標(biāo)及寬高
contentEntry.place(x=20, y=10, width=260, height=30)
?
# 按鈕顯示內(nèi)容
bvalue = ['CLC', '+', '-', '//', '0', '1', '2', '√', '3', '4', '5', '*', '6', '7', '8', '.', '9', '/', '**', '=']
index = 0
# 將按鈕進行 5x4 放置
for row in range(5):
? ? for col in range(4):
? ? ? ? d = bvalue[index]
? ? ? ? index += 1
? ? ? ? btnDigit = tkinter.Button(root, text=d, command=lambda x=d:onclick(x))
? ? ? ? btnDigit.place(x=20 + col * 70, y=50 + row * 30, width=50, height=20)
root.mainloop()③按鈕事件的響應(yīng)函數(shù)(可在評論區(qū)進行交流)
# 點擊事件
def onclick(btn):
? ? # 運算符
? ? operation = ('+', '-', '*', '/', '**', '//')
? ? # 獲取文本框中的內(nèi)容
? ? content = contentVar.get()
? ? # 如果已有內(nèi)容是以小數(shù)點開頭的,在前面加 0
? ? if content.startswith('.'):
? ? ? ? content = '0' + content ?# 字符串可以直接用+來增加字符
? ? # 根據(jù)不同的按鈕作出不同的反應(yīng)
? ? if btn in '0123456789':
? ? ? ? # 按下 0-9 在 content 中追加
? ? ? ? content += btn
? ? elif btn == '.':
? ? ? ? # 將 content 從 +-*/ 這些字符的地方分割開來
? ? ? ? lastPart = re.split(r'\+|-|\*|/', content)[-1]
? ? ? ? if '.' in lastPart:
? ? ? ? ? ? # 信息提示對話框
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '重復(fù)出現(xiàn)的小數(shù)點')
? ? ? ? ? ? return
? ? ? ? else:
? ? ? ? ? ? content += btn
? ? elif btn == 'CLC':
? ? ? ? # 清除文本框
? ? ? ? content = ''
? ? elif btn == '=':
? ? ? ? try:
? ? ? ? ? ? # 對輸入的表達式求值
? ? ? ? ? ? content = str(eval(content))
? ? ? ? except:
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '表達式有誤')
? ? ? ? ? ? return
? ? elif btn in operation:
? ? ? ? if content.endswith(operation):
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '不允許存在連續(xù)運算符')
? ? ? ? ? ? return
? ? ? ? content += btn
? ? elif btn == '√':
? ? ? ? # 從 . 處分割存入 n,n 是一個列表
? ? ? ? n = content.split('.')
? ? ? ? # 如果列表中所有的都是數(shù)字,就是為了檢查表達式是不是正確的
? ? ? ? if all(map(lambda x: x.isdigit(), n)):
? ? ? ? ? ? content = eval(content) ** 0.5
? ? ? ? else:
? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '表達式錯誤')
? ? ? ? ? ? return以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)將橫表和縱表任意轉(zhuǎn)換的兩種方法
在日常做數(shù)據(jù)分析,接收到最多的表格是縱表,每個字段變量都有很長數(shù)據(jù)的長表,我們稱之為縱向數(shù)據(jù),但是,有時候,我們也會遇到橫表,對于橫向數(shù)據(jù),我們會數(shù)據(jù)轉(zhuǎn)化,將其轉(zhuǎn)化為縱向數(shù)據(jù),感興趣的同學(xué)跟著小編一起來學(xué)習(xí)吧2023-12-12
Python構(gòu)建XML樹結(jié)構(gòu)的方法示例
這篇文章主要介紹了Python構(gòu)建XML樹結(jié)構(gòu)的方法,結(jié)合實例形式分析了Python創(chuàng)建與打印xml數(shù)結(jié)構(gòu)的實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Python中的并發(fā)處理之a(chǎn)syncio包使用的詳解
本篇文章主要介紹了Python中的并發(fā)處理之a(chǎn)syncio包使用的詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作,涉及Python正則爬取數(shù)據(jù)及針對mysql數(shù)據(jù)庫的存儲操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-06-06

