python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
本文實(shí)例為大家分享了python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
直接上代碼
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() # 設(shè)置窗口 標(biāo)題 window.title('簡(jiǎn)易計(jì)算器') # 設(shè)置窗口 寬高 window.geometry('400x300') # 添加user顯示屏幕背景 screen_area = tk.Frame(width='400', height='100', bg='#ddd') # 放置到window中 screen_area.pack() # 示例設(shè)置顯示的數(shù)據(jù)類 current_value = tk.StringVar() current_value.set(0) # 數(shù)字顯示框 # anchor ?文本相對(duì)于標(biāo)簽中心的位置 ? 默認(rèn)是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) # 按鍵區(qū)域 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python制作簡(jiǎn)易計(jì)算器功能
- python制作簡(jiǎn)單計(jì)算器功能
- python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Python?tkinter實(shí)現(xiàn)計(jì)算器功能
- python實(shí)現(xiàn)簡(jiǎn)易版計(jì)算器
- Python實(shí)現(xiàn)簡(jiǎn)單的四則運(yùn)算計(jì)算器
- python 簡(jiǎn)易計(jì)算器程序,代碼就幾行
- 利用Tkinter(python3.6)實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
- 基于python的Tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
- Pyqt實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
相關(guān)文章
Tensorflow Summary用法學(xué)習(xí)筆記
這篇文章主要介紹了Tensorflow Summary用法學(xué)習(xí)筆記,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01TensorFlow實(shí)現(xiàn)MLP多層感知機(jī)模型
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)MLP多層感知機(jī)模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Python異步編程之協(xié)程任務(wù)的調(diào)度操作實(shí)例分析
這篇文章主要介紹了Python異步編程之協(xié)程任務(wù)的調(diào)度操作,結(jié)合實(shí)例形式分析了Python異步編程中協(xié)程任務(wù)的調(diào)度相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02調(diào)整Jupyter notebook的啟動(dòng)目錄操作
這篇文章主要介紹了調(diào)整Jupyter notebook的啟動(dòng)目錄操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-04-04Python使用Pycrypto庫(kù)進(jìn)行RSA加密的方法詳解
RSA加密算法是一種強(qiáng)大的公鑰加密算法,安全性很高,這里我們來(lái)看一下Python使用Pycrypto庫(kù)進(jìn)行RSA加密的方法詳解,需要的朋友可以參考下2016-06-06Windows系統(tǒng)下pycharm中的pip換源
這篇文章主要介紹了Windows系統(tǒng)下pycharm中的pip換源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02