自學python---01_windows計算器開發
阿新 • • 發佈:2018-12-23
一 開發環境說明:
python3.5+wxpython包+math包
win10環境下開發
編譯工具:pycharm
二 執行介面展示:
三 開源共享:
1 #! /user/bin/env python 2 #coding=utf-8 3 import sys 4 sys.path.append('C:\Program Files\Python35\Lib\site-packages') 5 import wx 6 import math 7 8 class MyCalculator(wx.Frame):9 def __init__(self): 10 super().__init__(parent=None, title="計算器_chenyuyang", size=(480, 400), 11 style=wx.DEFAULT_FRAME_STYLE) 12 self.Center() 13 panel = wx.Panel(parent=self) 14 panel.SetBackgroundColour('LIME GREEN') 15 panel.Center()16 font = wx.Font(16, wx.ROMAN, wx.NORMAL, wx.BOLD, underline=False) 17 18 self.equation = "" 19 self.result = 0 20 self.ResultFlag = 0 21 22 self.textprint = wx.TextCtrl(panel, -1, '', style=wx.TE_MULTILINE) 23 self.textprint.SetFont(font) 24 self.textprint.BackgroundColour = 'white' 25 26 boxsize = wx.BoxSizer(wx.VERTICAL) 27 boxsize.Add(self.textprint, 1, wx.EXPAND) 28 gridsizer = wx.GridSizer(cols=5, rows=6, vgap=1, hgap=0) 29 30 self.btn0 = wx.Button(panel, label='0') 31 self.btn1 = wx.Button(panel, label='1') 32 self.btn2 = wx.Button(panel, label='2') 33 self.btn3 = wx.Button(panel, label='3') 34 self.btn4 = wx.Button(panel, label='4') 35 self.btn5 = wx.Button(panel, label='5') 36 self.btn6 = wx.Button(panel, label='6') 37 self.btn7 = wx.Button(panel, label='7') 38 self.btn8 = wx.Button(panel, label='8') 39 self.btn9 = wx.Button(panel, label='9') 40 self.btn10 = wx.Button(panel, label='^') 41 self.btn11 = wx.Button(panel, label='.') 42 self.btn12 = wx.Button(panel, label='=') 43 self.btn13 = wx.Button(panel, label='÷') 44 self.btn14 = wx.Button(panel, label='×') 45 self.btn15 = wx.Button(panel, label='-') 46 self.btn16 = wx.Button(panel, label='+') 47 self.btn17 = wx.Button(panel, label='Ans') 48 self.btn18 = wx.Button(panel, label='AC') 49 self.btn19 = wx.Button(panel, label='DEL') 50 self.btn20 = wx.Button(panel, label='sin') 51 self.btn21 = wx.Button(panel, label='cos') 52 self.btn22 = wx.Button(panel, label='tan') 53 self.btn23 = wx.Button(panel, label='(') 54 self.btn24 = wx.Button(panel, label=')') 55 self.btn25 = wx.Button(panel, label='π') 56 self.btn26 = wx.Button(panel, label='log') 57 self.btn27 = wx.Button(panel, label='ln') 58 self.btn28 = wx.Button(panel, label='sqrt') 59 self.btn29 = wx.Button(panel, label='mod') 60 61 self.btn0.SetFont(font) 62 self.btn1.SetFont(font) 63 self.btn2.SetFont(font) 64 self.btn3.SetFont(font) 65 self.btn4.SetFont(font) 66 self.btn5.SetFont(font) 67 self.btn6.SetFont(font) 68 self.btn7.SetFont(font) 69 self.btn8.SetFont(font) 70 self.btn9.SetFont(font) 71 self.btn10.SetFont(font) 72 self.btn11.SetFont(font) 73 self.btn12.SetFont(font) 74 self.btn13.SetFont(font) 75 self.btn14.SetFont(font) 76 self.btn15.SetFont(font) 77 self.btn16.SetFont(font) 78 self.btn17.SetFont(font) 79 self.btn18.SetFont(font) 80 self.btn19.SetFont(font) 81 self.btn20.SetFont(font) 82 self.btn21.SetFont(font) 83 self.btn22.SetFont(font) 84 self.btn23.SetFont(font) 85 self.btn24.SetFont(font) 86 self.btn25.SetFont(font) 87 self.btn26.SetFont(font) 88 self.btn27.SetFont(font) 89 self.btn28.SetFont(font) 90 self.btn29.SetFont(font) 91 92 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn0) 93 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn1) 94 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn2) 95 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn3) 96 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn4) 97 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn5) 98 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn6) 99 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn7) 100 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn8) 101 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn9) 102 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn10) 103 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn11) 104 self.Bind(wx.EVT_BUTTON, self.OnResult, self.btn12) 105 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn13) 106 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn14) 107 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn15) 108 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn16) 109 self.Bind(wx.EVT_BUTTON, self.OnAns, self.btn17) 110 self.Bind(wx.EVT_BUTTON, self.OnAc, self.btn18) 111 self.Bind(wx.EVT_BUTTON, self.OnDel, self.btn19) 112 self.Bind(wx.EVT_BUTTON, self.OnOp, self.btn20) 113 self.Bind(wx.EVT_BUTTON, self.OnOp, self.btn21) 114 self.Bind(wx.EVT_BUTTON, self.OnOp, self.btn22) 115 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn23) 116 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn24) 117 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn25) 118 self.Bind(wx.EVT_BUTTON, self.OnOp, self.btn26) 119 self.Bind(wx.EVT_BUTTON, self.OnOp, self.btn27) 120 self.Bind(wx.EVT_BUTTON, self.OnOp, self.btn28) 121 self.Bind(wx.EVT_BUTTON, self.OnAppend, self.btn29) 122 123 for i in range(0,30): 124 exec("self.btn%s.BackgroundColour = 'pale green'"%i) 125 126 self.btn18.BackgroundColour = "red" 127 128 gridsizer.AddMany([(self.btn18, 0, wx.EXPAND), 129 (self.btn10, 0, wx.EXPAND), 130 (self.btn28, 0, wx.EXPAND), 131 (self.btn29, 0, wx.EXPAND), 132 (self.btn19, 0, wx.EXPAND), 133 (self.btn26, 0, wx.EXPAND), 134 (self.btn23, 0, wx.EXPAND), 135 (self.btn24, 0, wx.EXPAND), 136 (self.btn27, 0, wx.EXPAND), 137 (self.btn13, 0, wx.EXPAND), 138 (self.btn20, 0, wx.EXPAND), 139 (self.btn7, 0, wx.EXPAND), 140 (self.btn8, 0, wx.EXPAND), 141 (self.btn9, 0, wx.EXPAND), 142 (self.btn14, 0, wx.EXPAND), 143 (self.btn21, 0, wx.EXPAND), 144 (self.btn4, 0, wx.EXPAND), 145 (self.btn5, 0, wx.EXPAND), 146 (self.btn6, 0, wx.EXPAND), 147 (self.btn15, 0, wx.EXPAND), 148 (self.btn22, 0, wx.EXPAND), 149 (self.btn1, 0, wx.EXPAND), 150 (self.btn2, 0, wx.EXPAND), 151 (self.btn3, 0, wx.EXPAND), 152 (self.btn16, 0, wx.EXPAND), 153 (self.btn25, 0, wx.EXPAND), 154 (self.btn17, 0, wx.EXPAND), 155 (self.btn0, 0, wx.EXPAND), 156 (self.btn11, 0, wx.EXPAND), 157 (self.btn12, 0, wx.EXPAND), ]) 158 159 boxsize.Add(gridsizer, 6, wx.EXPAND) 160 panel.SetSizerAndFit(boxsize) 161 self.textprint.SetValue(self.equation) 162 163 def OnAbout(self, event): 164 dlg = wx.MessageDialog(self, "This is a calculator written by Python3.6.", "About Program", wx.OK) 165 dlg.ShowModal() 166 dlg.Destroy() 167 168 def OnAppend(self, event): 169 ops = ['×', '÷', '+', '-', '^', 'mod', '.'] 170 eventbutton = event.GetEventObject() 171 label = eventbutton.GetLabel() 172 if label not in ops and self.ResultFlag != 0: 173 self.ResultFlag = 0 174 self.textprint.Clear() 175 self.result = 0 176 self.equation = "" 177 178 if label in ops and self.ResultFlag != 0: 179 self.equation = str(self.result) 180 self.ResultFlag = 0 181 182 self.equation += label 183 self.textprint.SetValue(self.equation) 184 185 def OnOp(self, event): 186 187 label = event.GetEventObject().GetLabel() + '(' 188 if self.ResultFlag != 0: 189 self.equation = label + str(self.result) 190 self.ResultFlag = 0 191 else: 192 self.equation += label 193 self.textprint.SetValue(self.equation) 194 195 def OnDel(self, event): 196 if '=' not in self.equation: 197 self.ResultFlag = 0 198 self.result = 0 199 self.equation = self.equation[:-1] 200 self.textprint.SetValue(self.equation) 201 202 def OnAc(self, event): 203 self.textprint.Clear() 204 self.result = 0 205 self.ResultFlag = 0 206 self.equation = "" 207 208 def OnExit(self, event): 209 self.Close(True) 210 211 def OnResult(self, event): 212 string = self.equation 213 if 'log' in string: 214 string = string.replace('log', 'log10') 215 MathFac = ['sqrt', 'sin', 'cos', 'tan', 'log10'] 216 for fac in MathFac: 217 if fac in string: 218 string = string.replace(fac, 'math.' + fac) 219 if 'ln' in string: 220 string = string.replace('ln', 'math.log') 221 if '^' in string: 222 string = string.replace('^', '**') 223 if '×' in string: 224 string = string.replace('×', '*') 225 if '÷' in string: 226 string = string.replace('÷', '/') 227 if 'π' in string: 228 string = string.replace('π', 'math.pi') 229 if 'mod' in string: 230 string = string.replace('mod', '%') 231 count1 = 0 232 count2 = 0 233 for i in string: 234 if i == '(': 235 count1 += 1 236 string += ')' 237 for i in string: 238 if i == ')': 239 count2 += 1 240 if count1 < count2: 241 for i in range(count2 - count1): 242 string = '(' + string 243 count1 = 0 244 count2 = 0 245 try: 246 self.result = round(eval(string), 10) 247 self.equation += '\n=' + str(self.result) 248 self.textprint.SetValue(self.equation) 249 self.ResultFlag = 1 250 251 except(SyntaxError, ValueError, ZeroDivisionError, TypeError, AttributeError): 252 dlg = wx.MessageDialog(self, '請輸入正確的等式!', '請注意', wx.OK | wx.ICON_INFORMATION) 253 dlg.ShowModal() 254 dlg.Destroy() 255 self.textprint.Clear() 256 self.ResultFlag = 0 257 self.equation = "" 258 259 def OnAns(self, event): 260 RawAnswer = self.result 261 self.equation = str(RawAnswer) 262 self.textprint.SetValue(self.equation) 263 264 265 class App(wx.App): 266 267 def OnInit(self): 268 self.frame = MyCalculator() 269 self.frame.Bind(wx.EVT_CLOSE, self.OnClose, self.frame) 270 self.frame.Show() 271 return True 272 273 def OnClose(self, event): 274 dlg = wx.MessageDialog(None, "是否要關閉視窗?", "請確認", wx.YES_NO | wx.ICON_QUESTION) 275 retCode = dlg.ShowModal() 276 if (retCode == wx.ID_YES): 277 self.frame.Destroy() 278 else: 279 pass 280 281 282 if __name__ == '__main__': 283 app = App() 284 app.MainLoop()
四 打包xe檔案下載地址:
百度網盤:https://pan.baidu.com/s/1t2hbnVnAwElvoDOL2zHv6Q