教你python tkinter實現簡單計算器功能
本文例項為大家分享了python tkinter實現簡單計算器的具體程式碼,供大家參考,具體內容如下
效果圖
直接上程式碼
import tkinter as tk input_num_ls = [] first_num = None calculator_method = None def get_num(ls): new_ls = [10 ** i * float(num) for i, num in enumerate(ls)] ls_sum = sum(new_ls) if int(ls_sum) == ls_sum: return int(ls_sum) else: return ls_sum def append_num(num): global input_num_ls if len(num) < 10: input_num_ls.append(num) else: input_num_ls.append(num[:10]) current_value.set(get_num(input_num_ls)) print(input_num_ls) def append_calculator(method): global input_num_ls, first_num, calculator_method calculator_method = method first_num = get_num(input_num_ls) input_num_ls = [] print('method', calculator_method) def calculator_result(): global first_num, input_num_ls, calculator_method second_num = get_num(input_num_ls) input_num_ls.clear() if calculator_method == '+': current_value.set(second_num + first_num) input_num_ls.append(str(second_num + first_num)) elif calculator_method == '-': current_value.set(first_num - second_num) input_num_ls.append(str(first_num - second_num)) elif calculator_method == '*': current_value.set(first_num * second_num) input_num_ls.append(str(second_num * first_num)) elif calculator_method == '/': current_value.set(first_num / second_num) input_num_ls.append(str(first_num / second_num)) print(first_num, second_num, calculator_method) def clear(): global first_num, input_num_ls, calculator_method first_num = None input_num_ls = [] calculator_method = None current_value.set(0) def func(): pass # 主體視窗 window = tk.Tk() # 設定視窗 標題 window.title('簡易計算器') # 設定視窗 寬高 window.geometry('400x300') # 新增user顯示螢幕背景 screen_area = tk.Frame(width='400', height='100', bg='#ddd') # 放置到window中 screen_area.pack() # 示例設定顯示的資料類 current_value = tk.StringVar() current_value.set(0) # 數字顯示框 # anchor 文字相對於標籤中心的位置 預設是center N S W E show_screen_label = tk.Label(screen_area, textvariable=current_value, bg='white', width='400', height='2', font={'黑體', 40, 'bold'}, anchor='e') show_screen_label.pack(padx=10, pady=6) # 按鍵區域 button_area = tk.Frame(width='300', height='300', bg='#ccc') button_area.pack(padx=10, pady=5) # 新增button tk.Button(button_area, text='C', width='5', height='1', command=lambda: clear()).grid(row='1', column='0') tk.Button(button_area, text='+', width='5', height='1', command=lambda: append_calculator('+')).grid(row='1', column='1') tk.Button(button_area, text='-', width='5', height='1', command=lambda: append_calculator('-')).grid(row='1', column='2') tk.Button(button_area, text='*', width='5', height='1', command=lambda: append_calculator('*')).grid(row='1', column='3') tk.Button(button_area, text='7', width='5', height='1', command=lambda: append_num('7')).grid(row='2', column='0') tk.Button(button_area, text='8', width='5', height='1', command=lambda: append_num('8')).grid(row='2', column='1') tk.Button(button_area, text='9', width='5', height='1', command=lambda: append_num('9')).grid(row='2', column='2') tk.Button(button_area, text='/', width='5', height='1', command=lambda: append_calculator('/')).grid(row='2', column='3') tk.Button(button_area, text='4', width='5', height='1', command=lambda: append_num('4')).grid(row='3', column='0') tk.Button(button_area, text='5', width='5', height='1', command=lambda: append_num('5')).grid(row='3', column='1') tk.Button(button_area, text='6', width='5', height='1', command=lambda: append_num('6')).grid(row='3', column='2') tk.Button(button_area, text='=', width='5', height='1', command=lambda: calculator_result()).grid(row='3', column='3') tk.Button(button_area, text='1', width='5', height='1', command=lambda: append_num('1')).grid(row='4', column='0') tk.Button(button_area, text='2', width='5', height='1', command=lambda: append_num('2')).grid(row='4', column='1') tk.Button(button_area, text='3', width='5', height='1', command=lambda: append_num('3')).grid(row='4', column='2') tk.Button(button_area, text='C', width='5', height='1', command=lambda: clear()).grid(row='4', column='3') window.mainloop()
以上就是本文的全部內容,希望對大家的學習有所幫助.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69901823/viewspace-2855014/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 1、簡單工廠模式實現計算器功能模式
- 16_簡單計算器實現
- Python Tkinter 簡單使用Python
- Java實現一個簡單的計算器Java
- 用java實現一個簡單的計算器Java
- 從0到1實現一個簡單計算器
- Python編寫一個簡單計算器Python
- C++實現簡易計算器C++
- python簡易計算器Python
- Python + Tkinter簡單實現註冊登入(連線本地MySQL資料庫)PythonMySql資料庫
- 【Flutter 實戰】簡約而不簡單的計算器Flutter
- python 實現計算器功能 輸入字串,輸出相應結果Python字串
- python如何實現簡單的爬蟲功能?Python學習教程!Python爬蟲
- 簡單混合運算的計算器
- 前端 JavaScript 實現一個簡易計算器前端JavaScript
- [20211220]sqlplus簡單計算器.txtSQL
- ACCESS 在資料表中實現簡單計算
- 教你如何運用python實現簡單檔案讀寫函式Python函式
- JavaScript簡單計算器程式碼分析JavaScript
- 簡單計算器(棧的應用)
- python——設計一個簡單的購房商貸月供計算器Python
- 手摸手教你實現一個簡單的PromisePromise
- Unity——計時器功能實現Unity
- Python實現簡單負載均衡Python負載
- python實現簡單猜單詞遊戲Python遊戲
- Python tkinter 實現 指令碼工具 GUI模版Python指令碼GUI
- java方法練習之簡單計算器Java
- 7-20 簡單計算器 (20分)
- 超簡單實現iOS列表的索引功能iOS索引
- android 簡單實現指紋識別功能Android
- Java Web簡單登陸功能的實現JavaWeb
- Vue實現簡單的購物車功能Vue
- 基於Python PIL實現簡單圖片格式轉化器Python
- 簡單幾行程式碼輕鬆實現大文字平行計算行程
- 利用函式指標,編寫一段簡潔的程式碼,實現計算器的功能函式指標
- Python使用TCP實現簡單對話PythonTCP
- HDU_1237 一個簡單的計算器
- IOS開發 製作簡單的計算器iOS