<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
上次我用我學習的python做一個簡易的計算器,我對計算器進行了,更改優化,變成了一個真正的計算器
1.計算機佈局
2.計算機執行
首先匯入模組:
Tkinter 作為 Python GUI 開發工具之一,它具有 GUI 軟體包的必備的常用功能。比如,它提供了十多種不同型別的視窗控制元件、視窗佈局管理器、事件處理機制等,加之其開發效率高、程式碼簡潔易讀
import tkinter as tk #Python3標準安裝包中自帶tkinter,即不用安裝,匯入即可使用。 #可以從命令列執行python -m tkinter會開啟一個視窗 #演示一個簡單的Tk介面,證明tkinter已正確安裝在系統上. #python -m tkinter
1、先完成整個計算機的佈局及其它輸出結果的一欄
前面完成整體佈局就細化計算器中的按鈕
1、前四行程式碼是定義按鈕的符號、長寬、字型、顏色
2、後四行是按鈕的間距
root = tk.Tk() root.title('計算器') root.geometry('295x280+100+100') #邊距 font = ('宋體', 20) font_16 = ('宋體', 16) root.attributes("-alpha", 0.9) #設定透明度 result_num = tk.StringVar() #賦予可變變數 result_num.set('') tk.Label(root, textvariable=result_num, font=font, height=2, width=20, justify=tk.LEFT, anchor=tk.SE ).grid(row=1, column=1, columnspan=4) #顯示結果的佈局 button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色 button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色 button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色 button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色 button_clear.grid(row=2, column=1, padx=4, pady=2) button_back.grid(row=2, column=2, padx=4, pady=2) button_division.grid(row=2, column=3, padx=4, pady=2) button_multiplication.grid(row=2, column=4, padx=4, pady=2) #定義按鈕位置 #依葫蘆畫瓢 button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_seven.grid(row=3, column=1, padx=4, pady=2) button_eight.grid(row=3, column=2, padx=4, pady=2) button_nine.grid(row=3, column=3, padx=4, pady=2) button_subtraction.grid(row=3, column=4, padx=4, pady=2) #依葫蘆畫瓢 button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_four.grid(row=4, column=1, padx=4, pady=2) button_five.grid(row=4, column=2, padx=4, pady=2) button_six.grid(row=4, column=3, padx=4, pady=2) button_addition.grid(row=4, column=4, padx=4, pady=2) #依葫蘆畫瓢 button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_one.grid(row=5, column=1, padx=4, pady=2) button_two.grid(row=5, column=2, padx=4, pady=2) button_three.grid(row=5, column=3, padx=4, pady=2) button_equal.grid(row=5, column=4, padx=4, pady=2) button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_zero1.grid(row=6, column=1, padx=4, pady=2) button_zero.grid(row=6, column=2, padx=4, pady=2) button_dot.grid(row=6, column=3, padx=4, pady=2) button_equal2.grid(row=6, column=4, padx=4, pady=2)
計算器佈局完成後(主體和細節) 賦予計算器計算能力
1、點選按鈕實現出現結果輸出欄及計算器的輸出欄
2、賦予事件能力
def click_button(x): print('X:t', x) result_num.set(result_num.get() + x)
點選按鈕可以輸入所對應的結果 並且呼叫lambda函數計算輸入結果並反饋
ambda 函數是匿名的: 所謂匿名函數,通俗地說就是沒有名字的函數。lambda函數沒有名字。 lambda 函數有輸入和輸出: 輸入是傳入到參數列argument_list的值,輸出是根據表示式expression計算得到的值。
button_one.config(command=lambda: click_button('1')) button_two.config(command=lambda: click_button('2')) button_three.config(command=lambda: click_button('3')) button_four.config(command=lambda: click_button('4')) button_five.config(command=lambda: click_button('5')) button_six.config(command=lambda: click_button('6')) button_seven.config(command=lambda: click_button('7')) button_eight.config(command=lambda: click_button('8')) button_nine.config(command=lambda: click_button('9')) button_zero.config(command=lambda: click_button('0')) button_dot.config(command=lambda: click_button('.')) button_addition.config(command=lambda: click_button('+')) button_subtraction.config(command=lambda: click_button('-')) button_multiplication.config(command=lambda: click_button('*')) button_division.config(command=lambda: click_button('/')) button_clear.config(command=lambda: btnClearDisplay()) button_equal.config(command=calculation)
呼叫calculation時間用來計算輸入結果 其中str來轉化字串.
呼叫btnClearDisplay清除來清空輸入結果
def calculation(): opt_str = result_num.get() result = eval(opt_str) result_num.set(str(result)) def btnClearDisplay(): opt_str = result_num.get() result = eval(str(opt_str)) result_num.set("")
最後一行程式碼不需要觸發lambda 因為不需要匿名函數也不用傳遞引數
button_clear.config(command=lambda: btnClearDisplay()) button_equal.config(command=calculation)
import tkinter as tk root = tk.Tk() root.title('計算器') root.geometry('295x280+100+100') font = ('宋體', 20) font_16 = ('宋體', 16) root.attributes("-alpha", 0.9) result_num = tk.StringVar() result_num.set('') tk.Label(root, textvariable=result_num, font=font, height=2, width=20, justify=tk.LEFT, anchor=tk.SE ).grid(row=1, column=1, columnspan=4) button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_clear.grid(row=2, column=1, padx=4, pady=2) button_back.grid(row=2, column=2, padx=4, pady=2) button_division.grid(row=2, column=3, padx=4, pady=2) button_multiplication.grid(row=2, column=4, padx=4, pady=2) button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_seven.grid(row=3, column=1, padx=4, pady=2) button_eight.grid(row=3, column=2, padx=4, pady=2) button_nine.grid(row=3, column=3, padx=4, pady=2) button_subtraction.grid(row=3, column=4, padx=4, pady=2) button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_four.grid(row=4, column=1, padx=4, pady=2) button_five.grid(row=4, column=2, padx=4, pady=2) button_six.grid(row=4, column=3, padx=4, pady=2) button_addition.grid(row=4, column=4, padx=4, pady=2) button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_one.grid(row=5, column=1, padx=4, pady=2) button_two.grid(row=5, column=2, padx=4, pady=2) button_three.grid(row=5, column=3, padx=4, pady=2) button_equal.grid(row=5, column=4, padx=4, pady=2) button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1') button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') button_zero1.grid(row=6, column=1, padx=4, pady=2) button_zero.grid(row=6, column=2, padx=4, pady=2) button_dot.grid(row=6, column=3, padx=4, pady=2) button_equal2.grid(row=6, column=4, padx=4, pady=2) def click_button(x): print('X:t', x) result_num.set(result_num.get() + x) def calculation(): opt_str = result_num.get() result = eval(opt_str) result_num.set(str(result)) def btnClearDisplay(): opt_str = result_num.get() result = eval(str(opt_str)) result_num.set("") button_one.config(command=lambda: click_button('1')) button_two.config(command=lambda: click_button('2')) button_three.config(command=lambda: click_button('3')) button_four.config(command=lambda: click_button('4')) button_five.config(command=lambda: click_button('5')) button_six.config(command=lambda: click_button('6')) button_seven.config(command=lambda: click_button('7')) button_eight.config(command=lambda: click_button('8')) button_nine.config(command=lambda: click_button('9')) button_zero.config(command=lambda: click_button('0')) button_dot.config(command=lambda: click_button('.')) button_addition.config(command=lambda: click_button('+')) button_subtraction.config(command=lambda: click_button('-')) button_multiplication.config(command=lambda: click_button('*')) button_division.config(command=lambda: click_button('/')) button_clear.config(command=lambda: btnClearDisplay()) button_equal.config(command=calculation) root.mainloop()
可以看到計算器的頁面特別好看,跟著我一起學python,成就感滿滿 !!!
到此這篇關於Python實現簡易計算器的範例程式碼的文章就介紹到這了,更多相關Python計算器內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45