一、完美計算器實驗一
1 import tkinter 2 import math 3 import tkinter.messagebox 4 5 class calculator: 6 #界面布局方法 7 def __init__(self): 8 #創建主界面,並且保存到成員屬性中 9 self.root = tkinter.Tk() 10 self.root.minsize(280, 450) 11 self.root.maxsize(280, 470) 12 self.root.title('小餅餅丶的簡易計算器1.0') 13 # 設置顯式面板的變量 14 self.result = tkinter.StringVar() 15 self.result.set(0) 16 # 設置一個全局變量 運算數字和f符號的列表 17 self.lists = [] 18 # 添加一個用於判斷是否按下運算符號的標志 19 self.ispresssign = False 20 # 界面布局 21 self.menus() 22 self.layout() 23 self.root.mainloop() 24 25 26 #計算器菜單界面擺放 27 def menus(self): 28 # 添加菜單 29 # 創建總菜單 30 allmenu = tkinter.Menu(self.root) 31 # 添加子菜單 32 filemenu = tkinter.Menu(allmenu, tearoff=0) 33 # 添加選項卡 34 filemenu.add_command(label='標准型(T) Alt+1', command=self.myfunc) 35 filemenu.add_command(label='科學型(S) Alt+2', command=self.myfunc) 36 filemenu.add_command(label='程序員(P) Alt+3', command=self.myfunc) 37 filemenu.add_command(label='統計信息(A) Alt+4', command=self.myfunc) 38 # 添加分割線 39 filemenu.add_separator() 40 # 添加選項卡 41 filemenu.add_command(label='歷史記錄(Y) Ctrl+H', command=self.myfunc) 42 filemenu.add_command(label='數字分組(I)', command=self.myfunc) 43 # 添加分割線 44 filemenu.add_separator() 45 # 添加選項卡 46 filemenu.add_command(label='基本(B) Ctrl+F4', command=self.myfunc) 47 filemenu.add_command(label='單位轉換(U) Ctrl+U', command=self.myfunc) 48 filemenu.add_command(label='日期計算(D) Ctrl+E', command=self.myfunc) 49 menu1 = tkinter.Menu(filemenu, tearoff=0) 50 menu1.add_command(label='抵押(M)', command=self.myfunc) 51 menu1.add_command(label='汽車租賃(V)', command=self.myfunc) 52 menu1.add_command(label='油耗(mpg)(F)', command=self.myfunc) 53 menu1.add_command(label='油耗(l/100km)(U)', command=self.myfunc) 54 filemenu.add_cascade(label='工作表(W)', menu=menu1) 55 allmenu.add_cascade(label='查看(V)', menu=filemenu) 56 57 # 添加子菜單2 58 editmenu = tkinter.Menu(allmenu, tearoff=0) 59 # 添加選項卡 60 editmenu.add_command(label='復制(C) Ctrl+C', command=self.myfunc) 61 editmenu.add_command(label='粘貼(V) Ctrl+V', command=self.myfunc) 62 # 添加分割線 63 editmenu.add_separator() 64 # 添加選項卡 65 menu2 = tkinter.Menu(filemenu, tearoff=0) 66 menu2.add_command(label='復制歷史記錄(I)', command=self.myfunc) 67 menu2.add_command(label='編輯(E) F2', command=self.myfunc) 68 menu2.add_command(label='取消編輯(N) Esc', command=self.myfunc) 69 menu2.add_command(label='清除(L) Ctrl+Shift+D', command=self.myfunc) 70 editmenu.add_cascade(label='歷史記錄(H)', menu=menu2) 71 allmenu.add_cascade(label='編輯(E)', menu=editmenu) 72 73 # 添加子菜單3 74 helpmenu = tkinter.Menu(allmenu, tearoff=0) 75 # 添加選項卡 76 helpmenu.add_command(label='查看幫助(V) F1', command=self.myfunc) 77 # 添加分割線 78 helpmenu.add_separator() 79 # 添加選項卡 80 helpmenu.add_command(label='關於計算器(A)', command=self.myfunc) 81 allmenu.add_cascade(label='幫助(H)', menu=helpmenu) 82 83 self.root.config(menu=allmenu) 84 85 86 #計算器主界面擺放 87 def layout(self): 88 # 顯示屏 89 result = tkinter.StringVar() 90 result.set(0) 91 show_label = tkinter.Label(self.root, bd=3, bg='white', font=('宋體', 30), anchor='e', textvariable=self.result) 92 show_label.place(x=5, y=20, width=270, height=70) 93 # 功能按鈕MC 94 button_mc = tkinter.Button(self.root, text='MC', command=self.wait) 95 button_mc.place(x=5, y=95, width=50, height=50) 96 # 功能按鈕MR 97 button_mr = tkinter.Button(self.root, text='MR', command=self.wait) 98 button_mr.place(x=60, y=95, width=50, height=50) 99 # 功能按鈕MS 100 button_ms = tkinter.Button(self.root, text='MS', command=self.wait) 101 button_ms.place(x=115, y=95, width=50, height=50) 102 # 功能按鈕M+ 103 button_mjia = tkinter.Button(self.root, text='M+', command=self.wait) 104 button_mjia.place(x=170, y=95, width=50, height=50) 105 # 功能按鈕M- 106 button_mjian = tkinter.Button(self.root, text='M-', command=self.wait) 107 button_mjian.place(x=225, y=95, width=50, height=50) 108 # 功能按鈕← 109 button_zuo = tkinter.Button(self.root, text='←', command=self.dele_one) 110 button_zuo.place(x=5, y=150, width=50, height=50) 111 # 功能按鈕CE 112 button_ce = tkinter.Button(self.root, text='CE', command=lambda: self.result.set(0)) 113 button_ce.place(x=60, y=150, width=50, height=50) 114 # 功能按鈕C 115 button_c = tkinter.Button(self.root, text='C', command=self.sweeppress) 116 button_c.place(x=115, y=150, width=50, height=50) 117 # 功能按鈕± 118 button_zf = tkinter.Button(self.root, text='±', command=self.zf) 119 button_zf.place(x=170, y=150, width=50, height=50) 120 # 功能按鈕√ 121 button_kpf = tkinter.Button(self.root, text='√', command=self.kpf) 122 button_kpf.place(x=225, y=150, width=50, height=50) 123 # 數字按鈕7 124 button_7 = tkinter.Button(self.root, text='7', command=lambda: self.pressnum('7')) 125 button_7.place(x=5, y=205, width=50, height=50) 126 # 數字按鈕8 127 button_8 = tkinter.Button(self.root, text='8', command=lambda: self.pressnum('8')) 128 button_8.place(x=60, y=205, width=50, height=50) 129 # 數字按鈕9 130 button_9 = tkinter.Button(self.root, text='9', command=lambda: self.pressnum('9')) 131 button_9.place(x=115, y=205, width=50, height=50) 132 # 功能按鈕/ 133 button_division = tkinter.Button(self.root, text='/', command=lambda: self.presscalculate('/')) 134 button_division.place(x=170, y=205, width=50, height=50) 135 # 功能按鈕% 136 button_remainder = tkinter.Button(self.root, text='//', command=lambda:self.presscalculate('//')) 137 button_remainder.place(x=225, y=205, width=50, height=50) 138 # 數字按鈕4 139 button_4 = tkinter.Button(self.root, text='4', command=lambda: self.pressnum('4')) 140 button_4.place(x=5, y=260, width=50, height=50) 141 # 數字按鈕5 142 button_5 = tkinter.Button(self.root, text='5', command=lambda: self.pressnum('5')) 143 button_5.place(x=60, y=260, width=50, height=50) 144 # 數字按鈕6 145 button_6 = tkinter.Button(self.root, text='6', command=lambda: self.pressnum('6')) 146 button_6.place(x=115, y=260, width=50, height=50) 147 # 功能按鈕* 148 button_multiplication = tkinter.Button(self.root, text='*', command=lambda: self.presscalculate('*')) 149 button_multiplication.place(x=170, y=260, width=50, height=50) 150 # 功能按鈕1/x 151 button_reciprocal = tkinter.Button(self.root, text='1/x', command=self.ds) 152 button_reciprocal.place(x=225, y=260, width=50, height=50) 153 # 數字按鈕1 154 button_1 = tkinter.Button(self.root, text='1', command=lambda: self.pressnum('1')) 155 button_1.place(x=5, y=315, width=50, height=50) 156 # 數字按鈕2 157 button_2 = tkinter.Button(self.root, text='2', command=lambda: self.pressnum('2')) 158 button_2.place(x=60, y=315, width=50, height=50) 159 # 數字按鈕3 160 button_3 = tkinter.Button(self.root, text='3', command=lambda: self.pressnum('3')) 161 button_3.place(x=115, y=315, width=50, height=50) 162 # 功能按鈕- 163 button_subtraction = tkinter.Button(self.root, text='-', command=lambda: self.presscalculate('-')) 164 button_subtraction.place(x=170, y=315, width=50, height=50) 165 # 功能按鈕= 166 button_equal = tkinter.Button(self.root, text='=', command=lambda: self.pressequal()) 167 button_equal.place(x=225, y=315, width=50, height=105) 168 # 數字按鈕0 169 button_0 = tkinter.Button(self.root, text='0', command=lambda: self.pressnum('0')) 170 button_0.place(x=5, y=370, width=105, height=50) 171 # 功能按鈕. 172 button_point = tkinter.Button(self.root, text='.', command=lambda: self.pressnum('.')) 173 button_point.place(x=115, y=370, width=50, height=50) 174 # 功能按鈕+ 175 button_plus = tkinter.Button(self.root, text='+', command=lambda: self.presscalculate('+')) 176 button_plus.place(x=170, y=370, width=50, height=50) 177 178 179 #計算器菜單功能 180 def myfunc(self): 181 tkinter.messagebox.showinfo('','程序員懶死在電腦前,打死也做不出的功能,只是裝飾而已~') 182 183 184 #數字方法 185 def pressnum(self,num): 186 # 全局化變量 187 # 判斷是否按下了運算符號 188 if self.ispresssign == False: 189 pass 190 else: 191 self.result.set(0) 192 # 重置運算符號的狀態 193 self.ispresssign = False 194 if num == '.': 195 num = '0.' 196 # 獲取面板中的原有數字 197 oldnum = self.result.get() 198 # 判斷界面數字是否為0 199 if oldnum == '0': 200 self.result.set(num) 201 else: 202 # 連接上新按下的數字 203 newnum = oldnum + num 204 205 # 將按下的數字寫到面板中 206 self.result.set(newnum) 207 208 209 #運算函數 210 def presscalculate(self,sign): 211 # 保存已經按下的數字和運算符號 212 # 獲取界面數字 213 num = self.result.get() 214 self.lists.append(num) 215 # 保存按下的操作符號 216 self.lists.append(sign) 217 # 設置運算符號為按下狀態 218 self.ispresssign = True 219 220 221 #獲取運算結果 222 def pressequal(self): 223 # 獲取所有的列表中的內容(之前的數字和操作) 224 # 獲取當前界面上的數字 225 curnum = self.result.get() 226 # 將當前界面的數字存入列表 227 self.lists.append(curnum) 228 # 將列表轉化為字符串 229 calculatestr = ''.join(self.lists) 230 # 使用eval執行字符串中的運算即可 231 endnum = eval(calculatestr) 232 # 將運算結果顯示在界面中 233 self.result.set(str(endnum)[:10]) 234 if self.lists != 0: 235 self.ispresssign = True 236 # 清空運算列表 237 self.lists.clear() 238 239 240 #暫未開發說明 241 def wait(self): 242 tkinter.messagebox.showinfo('','功能在努力的實現,請期待2.0版本的更新') 243 244 245 #←按鍵功能 246 def dele_one(self): 247 if self.result.get() == '' or self.result.get() == '0': 248 self.result.set('0') 249 return 250 else: 251 num = len(self.result.get()) 252 if num > 1: 253 strnum = self.result.get() 254 strnum = strnum[0:num - 1] 255 self.result.set(strnum) 256 else: 257 self.result.set('0') 258 259 260 #±按鍵功能 261 def zf(self): 262 strnum = self.result.get() 263 if strnum[0] == '-': 264 self.result.set(strnum[1:]) 265 elif strnum[0] != '-' and strnum != '0': 266 self.result.set('-' + strnum) 267 268 269 #1/x按鍵功能 270 def ds(self): 271 dsnum = 1 / int(self.result.get()) 272 self.result.set(str(dsnum)[:10]) 273 if self.lists != 0: 274 self.ispresssign = True 275 # 清空運算列表 276 self.lists.clear() 277 278 279 #C按鍵功能 280 def sweeppress(self): 281 self.lists.clear() 282 self.result.set(0) 283 284 285 #√按鍵功能 286 def kpf(self): 287 strnum = float(self.result.get()) 288 endnum = math.sqrt(strnum) 289 if str(endnum)[-1] == '0': 290 self.result.set(str(endnum)[:-2]) 291 else: 292 self.result.set(str(endnum)[:10]) 293 if self.lists != 0: 294 self.ispresssign = True 295 # 清空運算列表 296 self.lists.clear() 297 298 299 #實例化對象 300 mycalculator = calculator()
來源於https://www.cnblogs.com/xiaobingbing/p/8016997.html