Python簡易計算器製作方法程式碼詳解
阿新 • • 發佈:2020-01-09
主要用到的工具是Python中的Tkinter庫
比較簡單
直接上圖形介面和程式碼
引用Tkinter庫
from tkinter import *
建立主視窗物件
window=Tk() #設定視窗物件 window.title('counting machine') window.geometry("350x280") window['bg']='red'
建立標籤框以及標籤(將運算字串顯示在上面)
frame=LabelFrame(window,bg='yellow',width=350,height=50) frame.pack() frame.place(x=0,y=0) label=Label(frame,text="1+1=2",height=3,width=50,bg='yellow') label.pack() #顯示框
設定全域性變數字串s,按一個按鈕,將按鈕對應的運算子加到這個字串s中,最後利用eval函式進行計算。
global s s=""
按鈕0-9以及小數點的實現(大致思路都是一樣的)
#按鈕. def figure_dot(): global s s=s+"." label.config(text=s) btn0=Button(window,text=".",width=4,command=figure_dot,bg='yellow') btn0.place(x=150,y=220) #按鈕. #按鈕0 def figure_0(): global s s=s+"0" label.config(text=s) btn0=Button(window,text="0",command=figure_0,bg='yellow') btn0.place(x=80,y=220) #按鈕0 #按鈕1 def figure_1(): global s s=s+"1" label.config(text=s) btn1=Button(window,text="1",command=figure_1,bg='yellow') btn1.place(x=10,y=80) #按鈕1 #按鈕2 def figure_2(): global s s=s+"2" label.config(text=s) btn2=Button(window,text="2",command=figure_2,bg='yellow') btn2.place(x=80,y=80)#按鈕2 #按鈕3 def figure_3(): global s s=s+"3" label.config(text=s) btn3=Button(window,text="3",command=figure_3,bg='yellow') btn3.place(x=150,y=80)#按鈕3 #按鈕4 def figure_4(): global s s=s+"4" label.config(text=s) btn4=Button(window,text="4",command=figure_4,bg='yellow') btn4.place(x=10,y=130)#按鈕4 #按鈕5 def figure_5(): global s s=s+"5" label.config(text=s) btn5=Button(window,text="5",command=figure_5,bg='yellow') btn5.place(x=80,y=130)#按鈕5 #按鈕6 def figure_6(): global s s=s+"6" label.config(text=s) btn6=Button(window,text="6",command=figure_6,bg='yellow') btn6.place(x=150,y=130)#按鈕6 #按鈕7 def figure_7(): global s s=s+"7" label.config(text=s) btn7=Button(window,text="7",command=figure_7,bg='yellow') btn7.place(x=10,y=180)#按鈕7 #按鈕8 def figure_8(): global s s=s+"8" label.config(text=s) btn8=Button(window,text="8",command=figure_8,bg='yellow') btn8.place(x=80,y=180)#按鈕8 #按鈕9 def figure_9(): global s s=s+"9" label.config(text=s) btn9=Button(window,text="9",command=figure_9,bg='yellow') btn9.place(x=150,y=180)#按鈕9 運算子號的實現(±*/) #加法按鈕 def figure_addition(): global s s=s+"+" label.config(text=s) btn_add=Button(window,text="+",command=figure_addition,bg='yellow') btn_add.place(x=220,y=80)#加法按鈕 #減法按鈕 def figure_subtraction(): global s s=s+"-" label.config(text=s) btn_sub=Button(window,text="-",command=figure_subtraction,bg='yellow') btn_sub.place(x=220,y=130)#減法按鈕 #乘法按鈕 def figure_multiplication(): global s s=s+"*" label.config(text=s) btn_multi=Button(window,text="*",command=figure_multiplication,bg='yellow') btn_multi.place(x=290,y=80)#乘法按鈕 #除法按鈕 def figure_division(): global s s=s+"/" label.config(text=s) btn_divi=Button(window,text="/",command=figure_division,bg='yellow') btn_divi.place(x=290,y=130)#除法按鈕
清空視窗按鈕的實現
#清空按鈕 def figure_clear(): global s s="" label.config(text=s) btn_clear=Button(window,text="clear",command=figure_clear,bg='yellow') btn_clear.place(x=220,y=180)#清空按鈕
結果輸出的實現(eval函式)
#結果按鈕 def figure_value(): global s x=eval(s) s=str(x) label.config(text=s) btn_value=Button(window,text="=",command=figure_value,bg='yellow') btn_value.place(x=290,y=180)
顏色變換的實現(紅變粉)
def figure_colorchange(): window.config(bg="pink") btn_value=Button(window,text="color",command=figure_colorchange,bg='yellow') btn_value.place(x=10,y=220)#改變顏色 window.mainloop()
變換後
這個簡易計算器也就實現了,當然也可以加入其他的功能,如開方,乘冪等功能。
總結
以上所述是小編給大家介紹的Python簡易計算器製作方法程式碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!