本文實例講述了Python只用40行代碼編寫的計算器。分享給大家供大家參考,具體如下:
效果圖:

代碼:
from tkinter import *reset=Truedef buttonCallBack(event):  global label  global reset  num=event.widget['text']  if num=='C':    label['text']="0"    return  if num in "=":    label['text']=str(eval(label['text']))    reset=True    return  s=label['text']  if s=='0' or reset==True:    s=""    reset=False  label['text']=s+num#主窗口root=Tk()root.wm_title("計算器")#顯示欄1label=Label(root,text="0",background="white",anchor="e")label['width']=35label['height']=2label.grid(row=1,columnspan=4,sticky=W)#按鈕showText="789/456*123-0.C+"for i in range(4):  for j in range(4):    b=Button(root,text=showText[i*4+j],width=7)    b.grid(row=i+2,column=j)    b.bind("<button-1>",buttonCallBack)showText="()"for i in range(2):www.jb51.net  b=Button(root,text=showText[i],width=7)  b.grid(row=6,column=2+i)  b.bind("<button-1>",buttonCallBack)b=Button(root,text="=")b.grid(row=6,columnspan=2,sticky="we")b.bind("<button-1>",buttonCallBack)root.mainloop()</button-1></button-1></button-1>PS:這里再為大家推薦幾款計算工具供大家進一步參考借鑒:
在線一元函數(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue
在線計算器_標準計算器:
http://tools.jb51.net/jisuanqi/jsq
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答