面向对象简易计算器
import tkinter
import math
import tkinter.messagebox
class jsq:
#界面布局方法
def __init__(self):
#創建界面
self.root = tkinter.Tk()
self.root.minsize(280,420)
self.root.title('chy計算器')
#調用方法
self.showlabel()
self.myfunc()
self.root.resizable(width = False, height = False)
self.root.mainloop()
def myfunc(self):
print('執行操作')
#添加菜單
#創建總菜單
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+F',command = self.myfunc)
filemenu.add_command(label='單位轉換(U) Ctrl+U',command = self.myfunc)
filemenu.add_command(label='日期計算(D) Ctrl+E',command = self.myfunc)
#分割線
filemen= tkinter.Menu(allmenu,tearoff=0)
filemenu.add_cascade(label='工作表(W)',menu =filemen)
#創建工作表下的子菜單
filemen.add_command(label='抵押(M)',command = self.myfunc)
filemen.add_command(label='汽車租賃(D)',command = self.myfunc)
filemen.add_command(label='油耗(mpg)(F)',command = self.myfunc)
filemen.add_command(label='油耗(U)',command = self.myfunc)
#將查看菜單放入總菜單中
allmenu.add_cascade(label='查看(V)',menu=filemenu)
#創建編輯總菜單
editmenu= tkinter.Menu(allmenu,tearoff=0)
editmenu.add_command(label='復制(C) Ctrl+C',command= self.myfunc)
editmenu.add_command(label='粘貼(P) Ctrl+V',command= self.myfunc)
#分割線
editmenu.add_separator()
editmenu.add_command(label='歷史記錄(H)',command = self.myfunc)
#將編輯菜單加入總菜單
allmenu.add_cascade(label='編輯(E)',menu=editmenu)
#創建幫助菜單
helpmenu= tkinter.Menu(allmenu,tearoff=0)
helpmenu.add_command(label='查看幫助(V) Ctrl+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)
# 設置一個全局變量 運算數字和f符號的列表
lists = []
# 添加一個用于判斷是否按下運算符號的標志
isPressSign = False
# 添加一個等于號的標志
pressEqual = False
#操作函數
#數字函數
def pressNum(self,num):
#全局變量
global lists
global isPressSign
# 判斷是否按下了運算符號
if self.isPressSign == False:
pass
else:
self.result.set(0)
# 重置運算符號的狀態
self.isPressSign = False
# 獲取面板中的原有數字
oldnum = self.result.get()
# 判斷界面數字是否為0
if oldnum == '0':
self.result.set(num)
else:
# 鏈接上 新按下的數字
newnum = oldnum + num
# 將按下的數字寫到面板中
self.result.set(newnum)
# 運算函數
def pressCompute(self,sign):
# 保存已經按下的數字和運算符號
# 獲取界面數字
num = self.result.get()
self.lists.append(num)
# 保存按下的操作符號
self.lists.append(sign)
# 設置運算符號為按下狀態)
self.isPressSign = True
# 獲取運算結果
def pressEqual(self):
# 全局化操作
global lists
# 獲取所有的列表中的內容(之前的數字和操作)
# 獲取當前界面尚的數字
curnum = self.result.get()
# 將當前界面的數數字存入列表
self.lists.append(curnum)
# 將列表轉化為字符串
computeStr = ''.join(self.lists)
# 使用eval執行字符串中的運算即可
endNum = eval(computeStr)
# 將運算結果顯式到界面中
self.result.set(endNum)
# 清空運算列表
self.lists.clear()
#MC鍵
def pressMc(self):
tkinter.messagebox.showerror(title='MC鍵',message='無法定義按鍵')
#MR鍵
def pressMr(self):
tkinter.messagebox.showerror(title='MR鍵',message='無法定義按鍵')
#MS鍵
def pressMs(self):
tkinter.messagebox.showerror(title='MS鍵',message='無法定義按鍵')
#M+鍵
def pressAdd(self):
tkinter.messagebox.showerror(title='M+鍵',message='無法定義按鍵')
#M-鍵
def pressSub(self):
tkinter.messagebox.showerror(title='M-鍵',message='無法定義按鍵')
#刪除鍵(←)
def pressDelete(self):
#獲取當前數的長度
len1 = len(self.result.get())
#判斷長度是否大于1
if len1 > 1:
#每次執行按鍵功能,取片去除最后一位數,再輸出顯示
num1 = self.result.get()
num1 = num1[:len1-1]
self.result.set(num1)
# 清除鍵(CE)
def pressCE(self):
self.result.set(0)
#清除鍵(C)
def pressClear(self):
self.result.set(0)
#正負號鍵(±)
def pressSign(self):
# 獲取當前的數,乘-1后輸出到當前顯示
if float(self.result.get()) == 0:
self.result.set(0)
else:
self.result.set(-1*float(self.result.get()))
#根號鍵(√)
def pressSqrt(self):
#獲取當前的數,根平方后輸出到當前顯示
if float(self.result.get()) <= 0:
self.result.set('請輸入大于0的整數')
else:
self.result.set(math.sqrt(float(self.result.get())))
?
#倒數鍵(1/x)
def pressRec(self):
if float(self.result.get()) == 0:
self.result.set(0)
else:
self.result.set(1/float(self.result.get()))
?
#小數點鍵(.)
def pressDec(self,Del):
# 判斷小數點是否被按下
if '.' in self.result.get():
pass
else:
self.result.set(self.result.get() + Del)
# 判斷等號是否被按下
if self.pressEqual == True:
self.result.set('0' + Del)
# 重置運算符狀態
self.pressEqual = False
# 判斷運算符是否被按下
if self.isPressSign == True:
self.result.set('0' + Del)
#重置運算符狀態
self.isPressSign = False
def showlabel(self):
#界面布局
#按鈕show
self.result = tkinter.StringVar()
self.result.set(0)
show = tkinter.Label(self.root,textvariable=self.result,bg = 'white',font = ('黑體',20),anchor = 'e')
show.place(x = 5, y = 5,width = 270)
#第一行
btn = tkinter.Button(self.root,text = 'MC',command = self.pressMc)
btn.place(x = 5, y = 60, width = 50, height = 50)
btn = tkinter.Button(self.root,text = 'MR',command = self.pressMr)
btn.place(x = 60, y = 60, width = 50, height = 50)
btn = tkinter.Button(self.root,text = 'MS',command = self.pressMs)
btn.place(x = 115, y = 60, width = 50, height = 50)
btn = tkinter.Button(self.root,text = 'M+',command = self.pressAdd)
btn.place(x = 170, y = 60, width = 50, height = 50)
btn = tkinter.Button(self.root,text = 'M-',command = self.pressSub)
btn.place(x = 225, y = 60, width = 50, height = 50)
#第二行
btn = tkinter.Button(self.root,text = '←',command = self.pressDelete)
btn.place(x = 5, y = 120, width = 50, height = 50)
btn = tkinter.Button(self.root,text = 'CE',command= self.pressCE)
btn.place(x = 60, y = 120, width = 50, height = 50)
btn = tkinter.Button(self.root,text = 'C',command = self.pressClear)
btn.place(x = 115, y = 120, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '±',command= self.pressSign)
btn.place(x = 170, y = 120, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '√',command = self.pressSqrt)
btn.place(x = 225, y = 120, width = 50, height = 50)
#第三行
btn7 = tkinter.Button(self.root,text = 7,command = lambda : self.pressNum('7'))
btn7.place(x = 5, y = 180, width = 50, height = 50)
btn8 = tkinter.Button(self.root,text = 8,command = lambda : self.pressNum('8'))
btn8.place(x = 60, y = 180, width = 50, height = 50)
btn9 = tkinter.Button(self.root,text = 9,command = lambda : self.pressNum('9'))
btn9.place(x = 115, y = 180, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '/',command = lambda :self.pressCompute('/'))
btn.place(x = 170, y = 180, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '%',command =lambda:self.pressCompute('%'))
btn.place(x = 225, y = 180, width = 50, height = 50)
#第四行
btn4 = tkinter.Button(self.root,text = 4,command = lambda : self.pressNum('4'))
btn4.place(x = 5, y = 240, width = 50, height = 50)
btn5 = tkinter.Button(self.root,text = 5,command = lambda : self.pressNum('5'))
btn5.place(x = 60, y = 240, width = 50, height = 50)
btn6 = tkinter.Button(self.root,text = 6,command = lambda : self.pressNum('6'))
btn6.place(x = 115, y = 240, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '*',command = lambda :self.pressCompute('*'))
btn.place(x = 170, y = 240, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '1/x',command = self.pressRec)
btn.place(x = 225, y = 240, width = 50, height = 50)
#第五行
btn1 = tkinter.Button(self.root,text = 1,command = lambda : self.pressNum('1'))
btn1.place(x = 5, y = 300, width = 50, height = 50)
btn2 = tkinter.Button(self.root,text = 2,command = lambda : self.pressNum('2'))
btn2.place(x = 60, y = 300, width = 50, height = 50)
btn3 = tkinter.Button(self.root,text = 3,command = lambda : self.pressNum('3'))
btn3.place(x = 115, y = 300, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '-',command = lambda :self.pressCompute('-'))
btn.place(x = 170, y = 300, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '=',command = self.pressEqual)
btn.place(x = 225, y = 300, width = 50, height = 110)
#第六行
btn0 = tkinter.Button(self.root,text = 0,command = lambda : self.pressNum('0'))
btn0.place(x = 5, y = 360, width = 105, height = 50)
btn = tkinter.Button(self.root,text = '.',command =lambda: self.pressDec('.'))
btn.place(x = 115, y = 360, width = 50, height = 50)
btn = tkinter.Button(self.root,text = '+',command = lambda :self.pressCompute('+'))
btn.place(x = 170, y = 360, width = 50, height = 50)
calculator = jsq()
轉載于:https://www.cnblogs.com/cuihengyue/p/7906504.html
總結
- 上一篇: LInux在线安装JDK
- 下一篇: 黑盒测试实践(小组作业)每日例会记录——