import tkinter
import math
import tkinter.messagebox
class calculator:
#界面布局方法
def __init__(self):
#創建主界面,並且保存到成員屬性中
self.root = tkinter.Tk()
self.root.minsize(280, 450)
self.root.maxsize(280, 470)
self.root.title('小餅餅丶的簡易計算器1.0')
# 設置顯式面板的變量
self.result = tkinter.StringVar()
self.result.set(0)
# 設置一個全局變量 運算數字和f符號的列表
self.lists = []
# 添加一個用於判斷是否按下運算符號的標志
self.ispresssign = False
# 界面布局
self.menus()
self.layout()
self.root.mainloop()
#計算器菜單界面擺放
def menus(self):
# 添加菜單
# 創建總菜單
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='統計信息(A) Alt+4', command=self.myfunc)
# 添加分割線
filemenu.add_separator()
# 添加選項卡
filemenu.add_command(label='歷史記錄(Y) Ctrl+H', command=self.myfunc)
filemenu.add_command(label='數字分組(I)', command=self.myfunc)
# 添加分割線
filemenu.add_separator()
# 添加選項卡
filemenu.add_command(label='基本(B) Ctrl+F4', command=self.myfunc)
filemenu.add_command(label='單位轉換(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='關於計算器(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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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)
# 數字按鈕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('','程序員懶死在電腦前,打死也做不出的功能,只是裝飾而已~')
#數字方法
def pressnum(self,num):
# 全局化變量
# 判斷是否按下了運算符號
if self.ispresssign == False:
pass
else:
self.result.set(0)
# 重置運算符號的狀態
self.ispresssign = False
if num == '.':
num = '0.'
# 獲取面板中的原有數字
oldnum = self.result.get()
# 判斷界面數字是否為0
if oldnum == '0':
self.result.set(num)
else:
# 連接上新按下的數字
newnum = oldnum + num
# 將按下的數字寫到面板中
self.result.set(newnum)
#運算函數
def presscalculate(self,sign):
# 保存已經按下的數字和運算符號
# 獲取界面數字
num = self.result.get()
self.lists.append(num)
# 保存按下的操作符號
self.lists.append(sign)
# 設置運算符號為按下狀態
self.ispresssign = True
#獲取運算結果
def pressequal(self):
# 獲取所有的列表中的內容(之前的數字和操作)
# 獲取當前界面上的數字
curnum = self.result.get()
# 將當前界面的數字存入列表
self.lists.append(curnum)
# 將列表轉化為字符串
calculatestr = ''.join(self.lists)
# 使用eval執行字符串中的運算即可
endnum = eval(calculatestr)
# 將運算結果顯示在界面中
self.result.set(str(endnum)[:10])
if self.lists != 0:
self.ispresssign = True
# 清空運算列表
self.lists.clear()
#暫未開發說明
def wait(self):
tkinter.messagebox.showinfo('','功能在努力的實現,請期待2.0版本的更新')
#←按鍵功能
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()
#實例化對象
mycalculator = calculator()
只是做了個簡單的計算器,還有幾個按鈕的功能還未實現,當然也不乏一些BUG的存在,如果有興趣的小伙伴可以留言,一起完善這個計算器~
