import tkinter #匯入tkinter模組
root = tkinter.Tk()
root.minsize(280,500)
root.title('計算器')
#1.介面佈局
#顯示面板
result = tkinter.StringVar()
result.set(0) #顯示面板顯示結果1,用於顯示預設數字0
result2 = tkinter.StringVar() #顯示面板顯示結果2,用於顯示計算過程
result2.set('')
#顯示版
label = tkinter.Label(root,font = ('微軟雅黑',20),bg = '#EEE9E9',bd ='9',fg = '#828282',anchor = 'se',textvariable = result2)
label.place(width = 280,height = 170)
label2 = tkinter.Label(root,font = ('微軟雅黑',30),bg = '#EEE9E9',bd ='9',fg = 'black',anchor = 'se',textvariable = result)
label2.place(y = 170,width = 280,height = 60)
btn7 = tkinter.Button(root,text = '7',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('7'))
btn7.place(x = 0,y = 285,width = 70,height = 55)
btn8 = tkinter.Button(root,text = '8',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('8'))
btn8.place(x = 70,y = 285,width = 70,height = 55)
btn9 = tkinter.Button(root,text = '9',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('9'))
btn9.place(x = 140,y = 285,width = 70,height = 55)
btn4 = tkinter.Button(root,text = '4',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('4'))
btn4.place(x = 0,y = 340,width = 70,height = 55)
btn5 = tkinter.Button(root,text = '5',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('5'))
btn5.place(x = 70,y = 340,width = 70,height = 55)
btn6 = tkinter.Button(root,text = '6',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('6'))
btn6.place(x = 140,y = 340,width = 70,height = 55)
btn1 = tkinter.Button(root,text = '1',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('1'))
btn1.place(x = 0,y = 395,width = 70,height = 55)
btn2 = tkinter.Button(root,text = '2',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('2'))
btn2.place(x = 70,y = 395,width = 70,height = 55)
btn3 = tkinter.Button(root,text = '3',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('3'))
btn3.place(x = 140,y = 395,width = 70,height = 55)
btn0 = tkinter.Button(root,text = '0',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('0'))
btn0.place(x = 70,y = 450,width = 70,height = 55)
btnac = tkinter.Button(root,text = 'AC',bd = 0.5,font = ('黑體',20),fg = 'orange',command = lambda :pressCompute('AC'))
btnac.place(x = 0,y = 230,width = 70,height = 55)
btnback = tkinter.Button(root,text = '←',font = ('微軟雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:pressCompute('b'))
btnback.place(x = 70,y = 230,width = 70,height = 55)
btndivi = tkinter.Button(root,text = '÷',font = ('微軟雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:pressCompute('/'))
btndivi.place(x = 140,y = 230,width = 70,height = 55)
btnmul = tkinter.Button(root,text ='×',font = ('微軟雅黑',20),fg = "#4F4F4F",bd = 0.5,command = lambda:pressCompute('*'))
btnmul.place(x = 210,y = 230,width = 70,height = 55)
btnsub = tkinter.Button(root,text = '-',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('-'))
btnsub.place(x = 210,y = 285,width = 70,height = 55)
btnadd = tkinter.Button(root,text = '+',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('+'))
btnadd.place(x = 210,y = 340,width = 70,height = 55)
btnequ = tkinter.Button(root,text = '=',bg = 'orange',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda :pressEqual())
btnequ.place(x = 210,y = 395,width = 70,height = 110)
btnper = tkinter.Button(root,text = '%',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('%'))
btnper.place(x = 0,y = 450,width = 70,height = 55)
btnpoint = tkinter.Button(root,text = '.',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('.'))
btnpoint.place(x = 140,y = 450,width = 70,height = 55)
#操作函式
lists = [] #設定一個變數 儲存運算數字和符號的列表
isPressNum = False
#數字函式
def pressNum(num): #設定一個數字函式 判斷是否按下數字 並獲取數字將數字寫在顯示版上
global isPressSign
if isPressSign == False:
pass
else: #重新將運算子號狀態設定為否
result.set(0)
isPressSign = False
#判斷介面的數字是否為0
oldnum = result.get() #第一步
if oldnum =='0': #如過介面上數字為0 則獲取按下的數字
result.set(num)
else: #如果介面上的而數字不是0 則連結上新按下的數字
newnum = oldnum + num
result.set(newnum) #將按下的數字寫到面板中
#運算函式
def pressCompute(sign):
global lists
global isPressSign
num = result.get() #獲取介面數字
lists.append(num) #儲存介面獲取的數字到列表中
lists.append(sign) #講按下的運算子號儲存到列表中
isPressSign = True
if sign =='AC': #如果按下的是'AC'按鍵,則清空列表內容,講螢幕上的數字鍵設定為預設數字0
lists.clear()
result.set(0)
if sign =='b': #如果按下的是退格'',則選取當前數字第一位到倒數第二位
a = num[0:-1]
lists.clear()
result.set(a)
#獲取運算結果函式
def pressEqual():
global lists
global isPressSign
curnum = result.get() #設定當前數字變數,並獲取新增到列表
lists.append(curnum)
computrStr = ''.join(lists) #講列表內容用join命令將字串連結起來
endNum = eval(computrStr) #用eval命令運算字串中的內容
# a = str(endNum)
# b = '='+a #給運算結果前新增一個 '=' 顯示 不過這樣寫會有BUG 不能連續運算,這裡註釋,不要 =
# c = b[0:10] #所有的運算結果取9位數
result.set(endNum) #講運算結果顯示到螢幕1
result2.set(computrStr) #將運算過程顯示到螢幕2
lists.clear() #清空列表內容
root.mainloop()