python實(shí)現(xiàn)計(jì)算器功能
本文實(shí)例為大家分享了python計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
主要用到的工具是Python中的Tkinter庫(kù)
比較簡(jiǎn)單
直接上圖形界面和代碼

引用Tkinter庫(kù)
from tkinter import *
建立主窗口對(duì)象
window=Tk() #設(shè)置窗口對(duì)象
window.title('counting machine')
window.geometry("350x280")
window['bg']='red'
建立標(biāo)簽框以及標(biāo)簽(將運(yùn)算字符串顯示在上面)
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() #顯示框
設(shè)置全局變量字符串s,按一個(gè)按鈕,將按鈕對(duì)應(yīng)的運(yùn)算符加到這個(gè)字符串s中,最后利用eval函數(shù)進(jìn)行計(jì)算。
global s s=""
按鈕0-9以及小數(shù)點(diǎn)的實(shí)現(xiàn)(大致思路都是一樣的)
#按鈕. 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",width=4,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",width=4,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",width=4,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",width=4,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",width=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",width=4,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",width=4,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",width=4,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",width=4,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",width=4,command=figure_9,bg='yellow') btn9.place(x=150,y=180)#按鈕9
運(yùn)算符號(hào)的實(shí)現(xiàn)(±*/)
#加法按鈕 def figure_addition(): global s s=s+"+" label.config(text=s) btn_add=Button(window,text="+",width=4,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="-",width=4,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="*",width=4,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="/",width=4,command=figure_division,bg='yellow') btn_divi.place(x=290,y=130)#除法按鈕
清空窗口按鈕的實(shí)現(xiàn)
#清空按鈕 def figure_clear(): global s s="" label.config(text=s) btn_clear=Button(window,text="clear",width=4,command=figure_clear,bg='yellow') btn_clear.place(x=220,y=180)#清空按鈕
結(jié)果輸出的實(shí)現(xiàn)(eval函數(shù))
#結(jié)果按鈕 def figure_value(): global s x=eval(s) s=str(x) label.config(text=s) btn_value=Button(window,text="=",width=4,command=figure_value,bg='yellow') btn_value.place(x=290,y=180)
顏色變換的實(shí)現(xiàn)(紅變粉)
def figure_colorchange(): window.config(bg="pink") btn_value=Button(window,text="color",width=4,command=figure_colorchange,bg='yellow') btn_value.place(x=10,y=220)#改變顏色 window.mainloop()
變換后

這個(gè)簡(jiǎn)易計(jì)算器也就實(shí)現(xiàn)了,當(dāng)然也可以加入其他的功能,如開方,乘冪等功能。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python GUI計(jì)算器的實(shí)現(xiàn)
- python GUI模擬實(shí)現(xiàn)計(jì)算器
- 基于wxpython開發(fā)的簡(jiǎn)單gui計(jì)算器實(shí)例
- python 實(shí)現(xiàn)一個(gè)圖形界面的匯率計(jì)算器
- 用python實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器(完整DEMO)
- python編寫計(jì)算器功能
- Python正則表達(dá)式實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能示例
- Python實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能詳解
- python 實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(gui界面)
相關(guān)文章
python將dict中的unicode打印成中文實(shí)例
這篇文章主要介紹了python將dict中的unicode打印成中文實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Pandas之Dropna濾除缺失數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Pandas之Dropna濾除缺失數(shù)據(jù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
python GUI庫(kù)圖形界面開發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02
只用40行Python代碼就能寫出pdf轉(zhuǎn)word小工具
今天咱們介紹一個(gè)pdf轉(zhuǎn)word的免費(fèi)小工具,滿足這么一個(gè)不常見但是偶爾會(huì)出來煩人的需求文中有非常詳細(xì)的代碼示例,對(duì)小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
如何在Win10系統(tǒng)使用Python3連接Hive
這篇文章主要介紹了如何在Win10系統(tǒng)使用Python3連接Hive,幫助大家更好的利用python讀取數(shù)據(jù),進(jìn)行探索、分析和挖掘工作。感興趣的朋友可以了解下2020-10-10
Python中的標(biāo)簽編碼和獨(dú)熱編碼示例詳解
標(biāo)簽編碼是一種用于將分類列轉(zhuǎn)換為數(shù)值列的技術(shù),以便它們可以通過僅采用數(shù)值數(shù)據(jù)的機(jī)器學(xué)習(xí)模型進(jìn)行擬合,這篇文章主要介紹了Python中的標(biāo)簽編碼和獨(dú)熱編碼,需要的朋友可以參考下2023-07-07
Python和Pycharm 環(huán)境部署詳細(xì)步驟
Python環(huán)境搭建過程很多朋友都操作過,本次我們將向大家介紹Python和Pycharm 環(huán)境部署的流程,文章通過圖文的形式給大家展示一目了然一看就懂,需要的朋友參考下吧2021-06-06

