如何用python做计算软件_如何用Python写一个计算器软件 附带效果图
1 import tkinter #導入tkinter模塊
2
3 root =tkinter.Tk()4 root.minsize(280,500)5 root.title('李蛟龍的計算器')6
7
8 #1.界面布局
9 #顯示面板
10 result =tkinter.StringVar()11 result.set(0) #顯示面板顯示結果1,用于顯示默認數字0
12 result2 = tkinter.StringVar() #顯示面板顯示結果2,用于顯示計算過程
13 result2.set('')14 #顯示版
15 label = tkinter.Label(root,font = ('微軟雅黑',20),bg = '#EEE9E9',bd ='9',fg = '#828282',anchor = 'se',textvariable =result2)16 label.place(width = 280,height = 170)17 label2 = tkinter.Label(root,font = ('微軟雅黑',30),bg = '#EEE9E9',bd ='9',fg = 'black',anchor = 'se',textvariable =result)18 label2.place(y = 170,width = 280,height = 60)19
20
21
22
23 #數字鍵按鈕
24
25 btn7 = tkinter.Button(root,text = '7',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('7'))26 btn7.place(x = 0,y = 285,width = 70,height = 55)27 btn8 = tkinter.Button(root,text = '8',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('8'))28 btn8.place(x = 70,y = 285,width = 70,height = 55)29 btn9 = tkinter.Button(root,text = '9',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('9'))30 btn9.place(x = 140,y = 285,width = 70,height = 55)31
32 btn4 = tkinter.Button(root,text = '4',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('4'))33 btn4.place(x = 0,y = 340,width = 70,height = 55)34 btn5 = tkinter.Button(root,text = '5',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('5'))35 btn5.place(x = 70,y = 340,width = 70,height = 55)36 btn6 = tkinter.Button(root,text = '6',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('6'))37 btn6.place(x = 140,y = 340,width = 70,height = 55)38
39 btn1 = tkinter.Button(root,text = '1',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('1'))40 btn1.place(x = 0,y = 395,width = 70,height = 55)41 btn2 = tkinter.Button(root,text = '2',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('2'))42 btn2.place(x = 70,y = 395,width = 70,height = 55)43 btn3 = tkinter.Button(root,text = '3',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('3'))44 btn3.place(x = 140,y = 395,width = 70,height = 55)45 btn0 = tkinter.Button(root,text = '0',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('0'))46 btn0.place(x = 70,y = 450,width = 70,height = 55)47
48
49 #運算符號按鈕
50 btnac = tkinter.Button(root,text = 'AC',bd = 0.5,font = ('黑體',20),fg = 'orange',command = lambda :pressCompute('AC'))51 btnac.place(x = 0,y = 230,width = 70,height = 55)52 btnback = tkinter.Button(root,text = '←',font = ('微軟雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:pressCompute('b'))53 btnback.place(x = 70,y = 230,width = 70,height = 55)54 btndivi = tkinter.Button(root,text = '÷',font = ('微軟雅黑',20),fg = '#4F4F4F',bd = 0.5,command = lambda:pressCompute('/'))55 btndivi.place(x = 140,y = 230,width = 70,height = 55)56 btnmul = tkinter.Button(root,text ='×',font = ('微軟雅黑',20),fg = "#4F4F4F",bd = 0.5,command = lambda:pressCompute('*'))57 btnmul.place(x = 210,y = 230,width = 70,height = 55)58 btnsub = tkinter.Button(root,text = '-',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('-'))59 btnsub.place(x = 210,y = 285,width = 70,height = 55)60 btnadd = tkinter.Button(root,text = '+',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('+'))61 btnadd.place(x = 210,y = 340,width = 70,height = 55)62 btnequ = tkinter.Button(root,text = '=',bg = 'orange',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressEqual())63 btnequ.place(x = 210,y = 395,width = 70,height = 110)64 btnper = tkinter.Button(root,text = '%',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('%'))65 btnper.place(x = 0,y = 450,width = 70,height = 55)66 btnpoint = tkinter.Button(root,text = '.',font = ('微軟雅黑',20),fg = ('#4F4F4F'),bd = 0.5,command = lambda:pressCompute('.'))67 btnpoint.place(x = 140,y = 450,width = 70,height = 55)68
69
70
71
72 #操作函數
73 lists = [] #設置一個變量 保存運算數字和符號的列表
74 isPressSign = False #添加一個判斷是否按下運算符號的標志,假設默認沒有按下按鈕
75 isPressNum =False76 #數字函數
77 def pressNum(num): #設置一個數字函數 判斷是否按下數字 并獲取數字將數字寫在顯示版上
78 global lists #全局化lists和按鈕狀態isPressSign
79 globalisPressSign80 if isPressSign ==False:81 pass
82 else: #重新將運算符號狀態設置為否
83 result.set(0)84 isPressSign =False85
86 #判斷界面的數字是否為0
87 oldnum = result.get() #第一步
88 if oldnum =='0': #如過界面上數字為0 則獲取按下的數字
89 result.set(num)90 else: #如果界面上的而數字不是0 則鏈接上新按下的數字
91 newnum = oldnum +num92 result.set(newnum) #將按下的數字寫到面板中
93
94
95
96
97
98
99
100
101 #運算函數
102 defpressCompute(sign):103 globallists104 globalisPressSign105 num = result.get() #獲取界面數字
106 lists.append(num) #保存界面獲取的數字到列表中
107
108 lists.append(sign) #講按下的運算符號保存到列表中
109 isPressSign =True110
111 if sign =='AC': #如果按下的是'AC'按鍵,則清空列表內容,講屏幕上的數字鍵設置為默認數字0
112 lists.clear()113 result.set(0)114 if sign =='b': #如果按下的是退格‘’,則選取當前數字第一位到倒數第二位
115 a = num[0:-1]116 lists.clear()117 result.set(a)118
119
120
121 #獲取運算結果函數
122 defpressEqual():123 globallists124 globalisPressSign125
126
127 curnum = result.get() #設置當前數字變量,并獲取添加到列表
128 lists.append(curnum)129
130 computrStr = ''.join(lists) #講列表內容用join命令將字符串鏈接起來
131 endNum = eval(computrStr) #用eval命令運算字符串中的內容
132 #a = str(endNum)
133 #b = '='+a #給運算結果前添加一個 ‘=’ 顯示 不過這樣寫會有BUG 不能連續運算,這里注釋,不要 =
134 #c = b[0:10] #所有的運算結果取9位數
135 result.set(endNum) #講運算結果顯示到屏幕1
136 result2.set(computrStr) #將運算過程顯示到屏幕2
137 lists.clear() #清空列表內容
138
139
140
141
142 root.mainloop()
總結
以上是生活随笔為你收集整理的如何用python做计算软件_如何用Python写一个计算器软件 附带效果图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 自定义控件 焦点,and
- 下一篇: java 获取当前月份减1_java S