python實(shí)現(xiàn)計(jì)算器小功能
本文實(shí)例為大家分享了python實(shí)現(xiàn)計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
1. 案例介紹
本例利用 Python 開發(fā)一個(gè)可以進(jìn)行簡單的四則運(yùn)算的圖形化計(jì)算器,會(huì)用到 Tkinter 圖形組件進(jìn)行開發(fā)。主要知識點(diǎn):Python Tkinter 界面編程;計(jì)算器邏輯運(yùn)算實(shí)現(xiàn)。本例難度為初級,適合具有 Python 基礎(chǔ)和 Tkinter 組件編程知識的用戶學(xué)習(xí)。
2. 設(shè)計(jì)原理
要制作一個(gè)計(jì)算器,首先需要知道它由哪些部分組成。示意如下圖所示。
從結(jié)構(gòu)上來說,一個(gè)簡單的圖形界面,需要由界面組件、組件的事件監(jiān)聽器(響應(yīng)各類事件的邏輯)和具體的事件處理邏輯組成。界面實(shí)現(xiàn)的主要工作是創(chuàng)建各個(gè)界面組件對象,對其進(jìn)行初始化,以及控制各組件之間的層次關(guān)系和布局。
3. 示例效果
4. 示例源碼
import tkinter import math import tkinter.messagebox ? ? class Calculator(object): ? ? # 界面布局方法 ? ? def __init__(self): ? ? ? ? # 創(chuàng)建主界面,并且保存到成員屬性中 ? ? ? ? self.root = tkinter.Tk() ? ? ? ? self.root.minsize(280, 450) ? ? ? ? self.root.maxsize(280, 470) ? ? ? ? self.root.title('計(jì)算器') ? ? ? ? # 設(shè)置顯式面板的變量 ? ? ? ? self.result = tkinter.StringVar() ? ? ? ? self.result.set(0) ? ? ? ? # 設(shè)置一個(gè)全局變量 ?運(yùn)算數(shù)字和f符號的列表 ? ? ? ? self.lists = [] ? ? ? ? # 添加一個(gè)用于判斷是否按下運(yùn)算符號的標(biāo)志 ? ? ? ? self.ispresssign = False ? ? ? ? # 界面布局 ? ? ? ? self.menus() ? ? ? ? self.layout() ? ? ? ? self.root.mainloop() ? ? ? # 計(jì)算器菜單界面擺放 ? ? ? def menus(self): ? ? ? ? # 添加菜單 ? ? ? ? # 創(chuàng)建總菜單 ? ? ? ? allmenu = tkinter.Menu(self.root) ? ? ? ? # 添加子菜單 ? ? ? ? filemenu = tkinter.Menu(allmenu, tearoff=0) ? ? ? ? # 添加選項(xiàng)卡 ? ? ? ? filemenu.add_command( ? ? ? ? ? ? label='標(biāo)準(zhǔn)型(T) ? ? ? ? ? ?Alt+1', command=self.myfunc) ? ? ? ? filemenu.add_command( ? ? ? ? ? ? label='科學(xué)型(S) ? ? ? ? ? ?Alt+2', command=self.myfunc) ? ? ? ? filemenu.add_command( ? ? ? ? ? ? label='程序員(P) ? ? ? ? ? ?Alt+3', command=self.myfunc) ? ? ? ? filemenu.add_command(label='統(tǒng)計(jì)信息(A) ? ? ? ?Alt+4', command=self.myfunc) ? ? ? ? # 添加分割線 ? ? ? ? filemenu.add_separator() ? ? ? ? # 添加選項(xiàng)卡 ? ? ? ? filemenu.add_command(label='歷史記錄(Y) ? ? ?Ctrl+H', command=self.myfunc) ? ? ? ? filemenu.add_command(label='數(shù)字分組(I)', command=self.myfunc) ? ? ? ? # 添加分割線 ? ? ? ? filemenu.add_separator() ? ? ? ? # 添加選項(xiàng)卡 ? ? ? ? filemenu.add_command( ? ? ? ? ? ? label='基本(B) ? ? ? ? ? ? Ctrl+F4', command=self.myfunc) ? ? ? ? filemenu.add_command(label='單位轉(zhuǎn)換(U) ? ? ?Ctrl+U', command=self.myfunc) ? ? ? ? filemenu.add_command(label='日期計(jì)算(D) ? ? ?Ctrl+E', command=self.myfunc) ? ? ? ? menu1 = tkinter.Menu(filemenu, tearoff=0) ? ? ? ? menu1.add_command(label='抵押(M)', command=self.myfunc) ? ? ? ? menu1.add_command(label='汽車租賃(V)', command=self.myfunc) ? ? ? ? menu1.add_command(label='油耗(mpg)(F)', command=self.myfunc) ? ? ? ? menu1.add_command(label='油耗(l/100km)(U)', command=self.myfunc) ? ? ? ? filemenu.add_cascade(label='工作表(W)', menu=menu1) ? ? ? ? allmenu.add_cascade(label='查看(V)', menu=filemenu) ? ? ? ? ? # 添加子菜單2 ? ? ? ? editmenu = tkinter.Menu(allmenu, tearoff=0) ? ? ? ? # 添加選項(xiàng)卡 ? ? ? ? editmenu.add_command(label='復(fù)制(C) ? ? ? ? Ctrl+C', command=self.myfunc) ? ? ? ? editmenu.add_command(label='粘貼(V) ? ? ? ? Ctrl+V', command=self.myfunc) ? ? ? ? # 添加分割線 ? ? ? ? editmenu.add_separator() ? ? ? ? # 添加選項(xiàng)卡 ? ? ? ? menu2 = tkinter.Menu(filemenu, tearoff=0) ? ? ? ? menu2.add_command(label='復(fù)制歷史記錄(I)', command=self.myfunc) ? ? ? ? menu2.add_command( ? ? ? ? ? ? label='編輯(E) ? ? ? ? ? ? ? ? ? ? ?F2', command=self.myfunc) ? ? ? ? menu2.add_command(label='取消編輯(N) ? ? ? ? ? ?Esc', command=self.myfunc) ? ? ? ? menu2.add_command(label='清除(L) ? ?Ctrl+Shift+D', command=self.myfunc) ? ? ? ? editmenu.add_cascade(label='歷史記錄(H)', menu=menu2) ? ? ? ? allmenu.add_cascade(label='編輯(E)', menu=editmenu) ? ? ? ? ? # 添加子菜單3 ? ? ? ? helpmenu = tkinter.Menu(allmenu, tearoff=0) ? ? ? ? # 添加選項(xiàng)卡 ? ? ? ? helpmenu.add_command(label='查看幫助(V) ? ? ? F1', command=self.myfunc) ? ? ? ? # 添加分割線 ? ? ? ? helpmenu.add_separator() ? ? ? ? # 添加選項(xiàng)卡 ? ? ? ? helpmenu.add_command(label='關(guān)于計(jì)算器(A)', command=self.myfunc) ? ? ? ? allmenu.add_cascade(label='幫助(H)', menu=helpmenu) ? ? ? ? ? self.root.config(menu=allmenu) ? ? ? # 計(jì)算器主界面擺放 ? ? ? def layout(self): ? ? ? ? # 顯示屏 ? ? ? ? result = tkinter.StringVar() ? ? ? ? result.set(0) ? ? ? ? show_label = tkinter.Label(self.root, bd=3, bg='white', font=( ? ? ? ? ? ? '宋體', 30), anchor='e', textvariable=self.result) ? ? ? ? show_label.place(x=5, y=20, width=270, height=70) ? ? ? ? # 功能按鈕MC ? ? ? ? button_mc = tkinter.Button(self.root, text='MC', command=self.wait) ? ? ? ? button_mc.place(x=5, y=95, width=50, height=50) ? ? ? ? # 功能按鈕MR ? ? ? ? button_mr = tkinter.Button(self.root, text='MR', command=self.wait) ? ? ? ? button_mr.place(x=60, y=95, width=50, height=50) ? ? ? ? # 功能按鈕MS ? ? ? ? button_ms = tkinter.Button(self.root, text='MS', command=self.wait) ? ? ? ? button_ms.place(x=115, y=95, width=50, height=50) ? ? ? ? # 功能按鈕M+ ? ? ? ? button_mjia = tkinter.Button(self.root, text='M+', command=self.wait) ? ? ? ? button_mjia.place(x=170, y=95, width=50, height=50) ? ? ? ? # 功能按鈕M- ? ? ? ? button_mjian = tkinter.Button(self.root, text='M-', command=self.wait) ? ? ? ? button_mjian.place(x=225, y=95, width=50, height=50) ? ? ? ? # 功能按鈕← ? ? ? ? button_zuo = tkinter.Button(self.root, text='←', command=self.dele_one) ? ? ? ? button_zuo.place(x=5, y=150, width=50, height=50) ? ? ? ? # 功能按鈕CE ? ? ? ? button_ce = tkinter.Button( ? ? ? ? ? ? self.root, text='CE', command=lambda: self.result.set(0)) ? ? ? ? button_ce.place(x=60, y=150, width=50, height=50) ? ? ? ? # 功能按鈕C ? ? ? ? button_c = tkinter.Button(self.root, text='C', command=self.sweeppress) ? ? ? ? button_c.place(x=115, y=150, width=50, height=50) ? ? ? ? # 功能按鈕± ? ? ? ? button_zf = tkinter.Button(self.root, text='±', command=self.zf) ? ? ? ? button_zf.place(x=170, y=150, width=50, height=50) ? ? ? ? # 功能按鈕√ ? ? ? ? button_kpf = tkinter.Button(self.root, text='√', command=self.kpf) ? ? ? ? button_kpf.place(x=225, y=150, width=50, height=50) ? ? ? ? # 數(shù)字按鈕7 ? ? ? ? button_7 = tkinter.Button( ? ? ? ? ? ? self.root, text='7', command=lambda: self.pressnum('7')) ? ? ? ? button_7.place(x=5, y=205, width=50, height=50) ? ? ? ? # 數(shù)字按鈕8 ? ? ? ? button_8 = tkinter.Button( ? ? ? ? ? ? self.root, text='8', command=lambda: self.pressnum('8')) ? ? ? ? button_8.place(x=60, y=205, width=50, height=50) ? ? ? ? # 數(shù)字按鈕9 ? ? ? ? button_9 = tkinter.Button( ? ? ? ? ? ? self.root, text='9', command=lambda: self.pressnum('9')) ? ? ? ? button_9.place(x=115, y=205, width=50, height=50) ? ? ? ? # 功能按鈕/ ? ? ? ? button_division = tkinter.Button( ? ? ? ? ? ? self.root, text='/', command=lambda: self.presscalculate('/')) ? ? ? ? button_division.place(x=170, y=205, width=50, height=50) ? ? ? ? # 功能按鈕% ? ? ? ? button_remainder = tkinter.Button( ? ? ? ? ? ? self.root, text='//', command=lambda: self.presscalculate('//')) ? ? ? ? button_remainder.place(x=225, y=205, width=50, height=50) ? ? ? ? # 數(shù)字按鈕4 ? ? ? ? button_4 = tkinter.Button( ? ? ? ? ? ? self.root, text='4', command=lambda: self.pressnum('4')) ? ? ? ? button_4.place(x=5, y=260, width=50, height=50) ? ? ? ? # 數(shù)字按鈕5 ? ? ? ? button_5 = tkinter.Button( ? ? ? ? ? ? self.root, text='5', command=lambda: self.pressnum('5')) ? ? ? ? button_5.place(x=60, y=260, width=50, height=50) ? ? ? ? # 數(shù)字按鈕6 ? ? ? ? button_6 = tkinter.Button( ? ? ? ? ? ? self.root, text='6', command=lambda: self.pressnum('6')) ? ? ? ? button_6.place(x=115, y=260, width=50, height=50) ? ? ? ? # 功能按鈕* ? ? ? ? button_multiplication = tkinter.Button( ? ? ? ? ? ? self.root, text='*', command=lambda: self.presscalculate('*')) ? ? ? ? button_multiplication.place(x=170, y=260, width=50, height=50) ? ? ? ? # 功能按鈕1/x ? ? ? ? button_reciprocal = tkinter.Button( ? ? ? ? ? ? self.root, text='1/x', command=self.ds) ? ? ? ? button_reciprocal.place(x=225, y=260, width=50, height=50) ? ? ? ? # 數(shù)字按鈕1 ? ? ? ? button_1 = tkinter.Button( ? ? ? ? ? ? self.root, text='1', command=lambda: self.pressnum('1')) ? ? ? ? button_1.place(x=5, y=315, width=50, height=50) ? ? ? ? # 數(shù)字按鈕2 ? ? ? ? button_2 = tkinter.Button( ? ? ? ? ? ? self.root, text='2', command=lambda: self.pressnum('2')) ? ? ? ? button_2.place(x=60, y=315, width=50, height=50) ? ? ? ? # 數(shù)字按鈕3 ? ? ? ? button_3 = tkinter.Button( ? ? ? ? ? ? self.root, text='3', command=lambda: self.pressnum('3')) ? ? ? ? button_3.place(x=115, y=315, width=50, height=50) ? ? ? ? # 功能按鈕- ? ? ? ? button_subtraction = tkinter.Button( ? ? ? ? ? ? self.root, text='-', command=lambda: self.presscalculate('-')) ? ? ? ? button_subtraction.place(x=170, y=315, width=50, height=50) ? ? ? ? # 功能按鈕= ? ? ? ? button_equal = tkinter.Button( ? ? ? ? ? ? self.root, text='=', command=lambda: self.pressequal()) ? ? ? ? button_equal.place(x=225, y=315, width=50, height=105) ? ? ? ? # 數(shù)字按鈕0 ? ? ? ? button_0 = tkinter.Button( ? ? ? ? ? ? self.root, text='0', command=lambda: self.pressnum('0')) ? ? ? ? button_0.place(x=5, y=370, width=105, height=50) ? ? ? ? # 功能按鈕. ? ? ? ? button_point = tkinter.Button( ? ? ? ? ? ? self.root, text='.', command=lambda: self.pressnum('.')) ? ? ? ? button_point.place(x=115, y=370, width=50, height=50) ? ? ? ? # 功能按鈕+ ? ? ? ? button_plus = tkinter.Button( ? ? ? ? ? ? self.root, text='+', command=lambda: self.presscalculate('+')) ? ? ? ? button_plus.place(x=170, y=370, width=50, height=50) ? ? ? # 計(jì)算器菜單功能 ? ? ? def myfunc(self): ? ? ? ? tkinter.messagebox.showinfo('', '預(yù)留接口,學(xué)成之后,你是不是有沖動(dòng)添加該功能.') ? ? ? # 數(shù)字方法 ? ? def pressnum(self, num): ? ? ? ? # 全局化變量 ? ? ? ? # 判斷是否按下了運(yùn)算符號 ? ? ? ? if self.ispresssign == False: ? ? ? ? ? ? pass ? ? ? ? else: ? ? ? ? ? ? self.result.set(0) ? ? ? ? ? ? # 重置運(yùn)算符號的狀態(tài) ? ? ? ? ? ? self.ispresssign = False ? ? ? ? if num == '.': ? ? ? ? ? ? num = '0.' ? ? ? ? # 獲取面板中的原有數(shù)字 ? ? ? ? oldnum = self.result.get() ? ? ? ? # 判斷界面數(shù)字是否為0 ? ? ? ? if oldnum == '0': ? ? ? ? ? ? self.result.set(num) ? ? ? ? else: ? ? ? ? ? ? # 連接上新按下的數(shù)字 ? ? ? ? ? ? newnum = oldnum + num ? ? ? ? ? ? ? # 將按下的數(shù)字寫到面板中 ? ? ? ? ? ? self.result.set(newnum) ? ? ? # 運(yùn)算函數(shù) ? ? def presscalculate(self, sign): ? ? ? ? # 保存已經(jīng)按下的數(shù)字和運(yùn)算符號 ? ? ? ? # 獲取界面數(shù)字 ? ? ? ? num = self.result.get() ? ? ? ? self.lists.append(num) ? ? ? ? # 保存按下的操作符號 ? ? ? ? self.lists.append(sign) ? ? ? ? # 設(shè)置運(yùn)算符號為按下狀態(tài) ? ? ? ? self.ispresssign = True ? ? ? # 獲取運(yùn)算結(jié)果 ? ? ? def pressequal(self): ? ? ? ? # 獲取所有的列表中的內(nèi)容(之前的數(shù)字和操作) ? ? ? ? # 獲取當(dāng)前界面上的數(shù)字 ? ? ? ? curnum = self.result.get() ? ? ? ? # 將當(dāng)前界面的數(shù)字存入列表 ? ? ? ? self.lists.append(curnum) ? ? ? ? # 將列表轉(zhuǎn)化為字符串 ? ? ? ? calculatestr = ''.join(self.lists) ? ? ? ? # 使用eval執(zhí)行字符串中的運(yùn)算即可 ? ? ? ? endnum = eval(calculatestr) ? ? ? ? # 將運(yùn)算結(jié)果顯示在界面中 ? ? ? ? self.result.set(str(endnum)[:10]) ? ? ? ? if self.lists != 0: ? ? ? ? ? ? self.ispresssign = True ? ? ? ? # 清空運(yùn)算列表 ? ? ? ? self.lists.clear() ? ? ? # 暫未開發(fā)說明 ? ? ? def wait(self): ? ? ? ? tkinter.messagebox.showinfo('', '更新中......') ? ? ? # ←按鍵功能 ? ? ? def dele_one(self): ? ? ? ? if self.result.get() == '' or self.result.get() == '0': ? ? ? ? ? ? self.result.set('0') ? ? ? ? ? ? return ? ? ? ? else: ? ? ? ? ? ? num = len(self.result.get()) ? ? ? ? ? ? if num > 1: ? ? ? ? ? ? ? ? strnum = self.result.get() ? ? ? ? ? ? ? ? strnum = strnum[0:num - 1] ? ? ? ? ? ? ? ? self.result.set(strnum) ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? self.result.set('0') ? ? ? # ±按鍵功能 ? ? ? def zf(self): ? ? ? ? strnum = self.result.get() ? ? ? ? if strnum[0] == '-': ? ? ? ? ? ? self.result.set(strnum[1:]) ? ? ? ? elif strnum[0] != '-' and strnum != '0': ? ? ? ? ? ? self.result.set('-' + strnum) ? ? ? # 1/x按鍵功能 ? ? ? def ds(self): ? ? ? ? dsnum = 1 / int(self.result.get()) ? ? ? ? self.result.set(str(dsnum)[:10]) ? ? ? ? if self.lists != 0: ? ? ? ? ? ? self.ispresssign = True ? ? ? ? # 清空運(yùn)算列表 ? ? ? ? self.lists.clear() ? ? ? # C按鍵功能 ? ? ? def sweeppress(self): ? ? ? ? self.lists.clear() ? ? ? ? self.result.set(0) ? ? ? # √按鍵功能 ? ? ? def kpf(self): ? ? ? ? strnum = float(self.result.get()) ? ? ? ? endnum = math.sqrt(strnum) ? ? ? ? if str(endnum)[-1] == '0': ? ? ? ? ? ? self.result.set(str(endnum)[:-2]) ? ? ? ? else: ? ? ? ? ? ? self.result.set(str(endnum)[:10]) ? ? ? ? if self.lists != 0: ? ? ? ? ? ? self.ispresssign = True ? ? ? ? # 清空運(yùn)算列表 ? ? ? ? self.lists.clear() ? ? # 實(shí)例化對象 my_calculator = Calculator()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)簡易版計(jì)算器
- Python實(shí)現(xiàn)簡單的四則運(yùn)算計(jì)算器
- python 簡易計(jì)算器程序,代碼就幾行
- 利用Tkinter(python3.6)實(shí)現(xiàn)一個(gè)簡單計(jì)算器
- 基于python的Tkinter實(shí)現(xiàn)一個(gè)簡易計(jì)算器
- Python設(shè)計(jì)實(shí)現(xiàn)的計(jì)算器功能完整實(shí)例
- 僅用50行代碼實(shí)現(xiàn)一個(gè)Python編寫的計(jì)算器的教程
- Python只用40行代碼編寫的計(jì)算器實(shí)例
- Python實(shí)現(xiàn)的簡單計(jì)算器功能詳解
- Python PyQt5實(shí)現(xiàn)的簡易計(jì)算器功能示例
相關(guān)文章
Python多進(jìn)程共享numpy 數(shù)組的方法
這篇文章主要介紹了Python多進(jìn)程共享numpy 數(shù)組的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07openCV中值濾波和均值濾波的代碼實(shí)現(xiàn)
在我們生活中的有很多時(shí)候都可以用到濾波,例如美顏的磨皮功能,本文就詳細(xì)的介紹了openCV中值濾波和均值濾波的代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03PyTorch 中的 torch.utils.data 解析(推薦)
這篇文章主要介紹了PyTorch?torch.utils.data.Dataset概述案例詳解,主要介紹對?torch.utils.data.Dataset?的理解,需要的朋友可以參考下2023-02-02Python的爬蟲框架scrapy用21行代碼寫一個(gè)爬蟲
最近在學(xué)習(xí)Python的爬蟲框架scrapy,通過爬取線報(bào)網(wǎng)站后發(fā)現(xiàn)整個(gè)過程還是挺值得學(xué)習(xí)的,所以下面這篇文章主要就給大家介紹了Python的爬蟲框架scrapy利用21行代碼寫一個(gè)爬蟲的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04