python實現(xiàn)計算器小功能
本文實例為大家分享了python實現(xiàn)計算器功能的具體代碼,供大家參考,具體內(nèi)容如下
1. 案例介紹
本例利用 Python 開發(fā)一個可以進行簡單的四則運算的圖形化計算器,會用到 Tkinter 圖形組件進行開發(fā)。主要知識點:Python Tkinter 界面編程;計算器邏輯運算實現(xiàn)。本例難度為初級,適合具有 Python 基礎(chǔ)和 Tkinter 組件編程知識的用戶學習。
2. 設(shè)計原理
要制作一個計算器,首先需要知道它由哪些部分組成。示意如下圖所示。

從結(jié)構(gòu)上來說,一個簡單的圖形界面,需要由界面組件、組件的事件監(jiān)聽器(響應(yīng)各類事件的邏輯)和具體的事件處理邏輯組成。界面實現(xiàn)的主要工作是創(chuàng)建各個界面組件對象,對其進行初始化,以及控制各組件之間的層次關(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('計算器')
? ? ? ? # 設(shè)置顯式面板的變量
? ? ? ? self.result = tkinter.StringVar()
? ? ? ? self.result.set(0)
? ? ? ? # 設(shè)置一個全局變量 ?運算數(shù)字和f符號的列表
? ? ? ? self.lists = []
? ? ? ? # 添加一個用于判斷是否按下運算符號的標志
? ? ? ? self.ispresssign = False
? ? ? ? # 界面布局
? ? ? ? self.menus()
? ? ? ? self.layout()
? ? ? ? self.root.mainloop()
?
? ? # 計算器菜單界面擺放
?
? ? def menus(self):
? ? ? ? # 添加菜單
? ? ? ? # 創(chuàng)建總菜單
? ? ? ? allmenu = tkinter.Menu(self.root)
? ? ? ? # 添加子菜單
? ? ? ? filemenu = tkinter.Menu(allmenu, tearoff=0)
? ? ? ? # 添加選項卡
? ? ? ? filemenu.add_command(
? ? ? ? ? ? label='標準型(T) ? ? ? ? ? ?Alt+1', command=self.myfunc)
? ? ? ? filemenu.add_command(
? ? ? ? ? ? label='科學型(S) ? ? ? ? ? ?Alt+2', command=self.myfunc)
? ? ? ? filemenu.add_command(
? ? ? ? ? ? label='程序員(P) ? ? ? ? ? ?Alt+3', command=self.myfunc)
? ? ? ? filemenu.add_command(label='統(tǒng)計信息(A) ? ? ? ?Alt+4', command=self.myfunc)
? ? ? ? # 添加分割線
? ? ? ? filemenu.add_separator()
? ? ? ? # 添加選項卡
? ? ? ? filemenu.add_command(label='歷史記錄(Y) ? ? ?Ctrl+H', command=self.myfunc)
? ? ? ? filemenu.add_command(label='數(shù)字分組(I)', command=self.myfunc)
? ? ? ? # 添加分割線
? ? ? ? filemenu.add_separator()
? ? ? ? # 添加選項卡
? ? ? ? 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='日期計算(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)
? ? ? ? # 添加選項卡
? ? ? ? editmenu.add_command(label='復制(C) ? ? ? ? Ctrl+C', command=self.myfunc)
? ? ? ? editmenu.add_command(label='粘貼(V) ? ? ? ? Ctrl+V', command=self.myfunc)
? ? ? ? # 添加分割線
? ? ? ? editmenu.add_separator()
? ? ? ? # 添加選項卡
? ? ? ? menu2 = tkinter.Menu(filemenu, tearoff=0)
? ? ? ? menu2.add_command(label='復制歷史記錄(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)
? ? ? ? # 添加選項卡
? ? ? ? helpmenu.add_command(label='查看幫助(V) ? ? ? F1', command=self.myfunc)
? ? ? ? # 添加分割線
? ? ? ? helpmenu.add_separator()
? ? ? ? # 添加選項卡
? ? ? ? helpmenu.add_command(label='關(guān)于計算器(A)', command=self.myfunc)
? ? ? ? allmenu.add_cascade(label='幫助(H)', menu=helpmenu)
?
? ? ? ? self.root.config(menu=allmenu)
?
? ? # 計算器主界面擺放
?
? ? 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)
?
? ? # 計算器菜單功能
?
? ? def myfunc(self):
? ? ? ? tkinter.messagebox.showinfo('', '預(yù)留接口,學成之后,你是不是有沖動添加該功能.')
?
? ? # 數(shù)字方法
? ? def pressnum(self, num):
? ? ? ? # 全局化變量
? ? ? ? # 判斷是否按下了運算符號
? ? ? ? if self.ispresssign == False:
? ? ? ? ? ? pass
? ? ? ? else:
? ? ? ? ? ? self.result.set(0)
? ? ? ? ? ? # 重置運算符號的狀態(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)
?
? ? # 運算函數(shù)
? ? def presscalculate(self, sign):
? ? ? ? # 保存已經(jīng)按下的數(shù)字和運算符號
? ? ? ? # 獲取界面數(shù)字
? ? ? ? num = self.result.get()
? ? ? ? self.lists.append(num)
? ? ? ? # 保存按下的操作符號
? ? ? ? self.lists.append(sign)
? ? ? ? # 設(shè)置運算符號為按下狀態(tài)
? ? ? ? self.ispresssign = True
?
? ? # 獲取運算結(jié)果
?
? ? def pressequal(self):
? ? ? ? # 獲取所有的列表中的內(nèi)容(之前的數(shù)字和操作)
? ? ? ? # 獲取當前界面上的數(shù)字
? ? ? ? curnum = self.result.get()
? ? ? ? # 將當前界面的數(shù)字存入列表
? ? ? ? self.lists.append(curnum)
? ? ? ? # 將列表轉(zhuǎn)化為字符串
? ? ? ? calculatestr = ''.join(self.lists)
? ? ? ? # 使用eval執(zhí)行字符串中的運算即可
? ? ? ? endnum = eval(calculatestr)
? ? ? ? # 將運算結(jié)果顯示在界面中
? ? ? ? self.result.set(str(endnum)[:10])
? ? ? ? if self.lists != 0:
? ? ? ? ? ? self.ispresssign = True
? ? ? ? # 清空運算列表
? ? ? ? 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
? ? ? ? # 清空運算列表
? ? ? ? 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
? ? ? ? # 清空運算列表
? ? ? ? self.lists.clear()
?
?
# 實例化對象
my_calculator = Calculator()以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pandas創(chuàng)建DataFrame的7種方法小結(jié)
這篇文章主要介紹了pandas創(chuàng)建DataFrame的7種方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
python數(shù)據(jù)結(jié)構(gòu):數(shù)據(jù)類型
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)中的數(shù)據(jù)類型,在?Python?以及其他所有面向?qū)ο缶幊陶Z言中,類都是對數(shù)據(jù)的構(gòu)成(狀態(tài))以及數(shù)據(jù)?能做什么(行為)的描述,下面我們就來你看看python數(shù)據(jù)結(jié)構(gòu)中的數(shù)據(jù)類型商務(wù)詳細介紹,需要的小伙伴可以參考一下2021-12-12
python中關(guān)于對super()函數(shù)疑問解惑
Python中的super()是用于調(diào)用父類(或父類的父類...)方法的函數(shù),主要用于多繼承,單繼承問題不大,下面這篇文章主要給大家介紹了關(guān)于python中關(guān)于對super()函數(shù)疑問解惑的相關(guān)資料,需要的朋友可以參考下2022-08-08

