基于Python+Tkinter實(shí)現(xiàn)一個(gè)簡易計(jì)算器
設(shè)計(jì)原理
從結(jié)構(gòu)上來說,一個(gè)簡單的圖形界面,需要由界面組件、組件的事件監(jiān)聽器(響應(yīng)各類事件的邏輯)和具體的事件處理邏輯組成。界面實(shí)現(xiàn)的主要工作是創(chuàng)建各個(gè)界面組件對象,對其進(jìn)行初始化,以及控制各組件之間的層次關(guān)系和布局
示例效果

主要代碼
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é)成之后,你是不是有沖動添加該功能.')
# 數(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()
以上就是基于Python+Tkinter實(shí)現(xiàn)一個(gè)簡易計(jì)算器的詳細(xì)內(nèi)容,更多關(guān)于Python Tkinter計(jì)算器的資料請關(guān)注腳本之家其它相關(guān)文章!
- 如何利用python的tkinter實(shí)現(xiàn)一個(gè)簡單的計(jì)算器
- Python tkinter實(shí)現(xiàn)簡單加法計(jì)算器代碼實(shí)例
- Python+tkinter使用40行代碼實(shí)現(xiàn)計(jì)算器功能
- Python Tkinter實(shí)現(xiàn)簡易計(jì)算器功能
- python使用tkinter實(shí)現(xiàn)簡單計(jì)算器
- Python+tkinter使用80行代碼實(shí)現(xiàn)一個(gè)計(jì)算器實(shí)例
- Python編程使用tkinter模塊實(shí)現(xiàn)計(jì)算器軟件完整代碼示例
- 基于python的Tkinter實(shí)現(xiàn)一個(gè)簡易計(jì)算器
相關(guān)文章
終于明白tf.reduce_sum()函數(shù)和tf.reduce_mean()函數(shù)用法
這篇文章主要介紹了終于明白tf.reduce_sum()函數(shù)和tf.reduce_mean()函數(shù)用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Python實(shí)戰(zhàn)購物車項(xiàng)目的實(shí)現(xiàn)參考
今天小編就為大家分享一篇關(guān)于Python實(shí)戰(zhàn)購物車項(xiàng)目的實(shí)現(xiàn)參考,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
pandas 數(shù)據(jù)結(jié)構(gòu)之Series的使用方法
這篇文章主要介紹了pandas 數(shù)據(jù)結(jié)構(gòu)之Series的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
使用python查找替換PowerPoint演示文稿中的文本
演示文稿已成為商務(wù)會議、學(xué)術(shù)報(bào)告和教育培訓(xùn)中不可或缺的一部分,而PowerPoint演示文稿作為行業(yè)標(biāo)準(zhǔn)工具,更是承載著無數(shù)創(chuàng)意與信息的載體,本文將介紹如何使用Python來精確查找并替換PowerPoint演示文稿中的文本,需要的朋友可以參考下2024-07-07
python爬蟲搭配起B(yǎng)ilibili唧唧的流程分析
這篇文章主要介紹了python爬蟲搭配起B(yǎng)ilibili唧唧的流程分析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Python使用wget實(shí)現(xiàn)下載網(wǎng)絡(luò)文件功能示例
這篇文章主要介紹了Python使用wget實(shí)現(xiàn)下載網(wǎng)絡(luò)文件功能,簡單介紹了wget安裝以及Python使用wget下載tar格式網(wǎng)絡(luò)文件并進(jìn)行解壓處理相關(guān)操作技巧,需要的朋友可以參考下2018-05-05

