當前位置:首頁 » 編程語言 » python簡單計算器

python簡單計算器

發布時間: 2025-01-11 03:15:13

python計算器

python計算器實現:

wx.TextCtrl(文本輸入控制項),這個作為計算器的顯示屏,點擊數字時用AppendText方法將數字添加至文本末尾,要取出數字時用GetValue方法,點擊運算符時要將文本框中的數字清空(先存儲)用Clear方法然後還有將操作符保存,點擊等號後要將第二個操作數存儲,然後用int方法將字元串轉換為數字,計算後用SetValue方法將結果送入文本框,具體代碼如下:

importwx
#beginwxGlade:extracode
#endwxGlade
classMyFrame(wx.Frame):
def__init__(self,*args,**kwds):
#beginwxGlade:MyFrame.__init__
kwds["style"]=wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self,*args,**kwds)
self.text_ctrl_1=wx.TextCtrl(self,-1,"",style=wx.TE_READONLY)
self.button_37=wx.Button(self,-1,"1")
self.button_38=wx.Button(self,-1,"2")
self.button_39=wx.Button(self,-1,"3")
self.button_40=wx.Button(self,-1,"+")
self.button_41=wx.Button(self,-1,"4")
self.button_42=wx.Button(self,-1,"5")
self.button_43=wx.Button(self,-1,"6")
self.button_44=wx.Button(self,-1,"-")
self.button_46=wx.Button(self,-1,"7")
self.button_45=wx.Button(self,-1,"8")
self.button_47=wx.Button(self,-1,"9")
self.button_48=wx.Button(self,-1,"x")
self.button_49=wx.Button(self,-1,"C")
self.button_50=wx.Button(self,-1,"0")
self.button_51=wx.Button(self,-1,"=")
self.button_52=wx.Button(self,-1,"/")
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_BUTTON,self.bu1,self.button_37)
self.Bind(wx.EVT_BUTTON,self.bu2,self.button_38)
self.Bind(wx.EVT_BUTTON,self.bu3,self.button_39)
self.Bind(wx.EVT_BUTTON,self.bu_plus,self.button_40)
self.Bind(wx.EVT_BUTTON,self.bu4,self.button_41)
self.Bind(wx.EVT_BUTTON,self.bu5,self.button_42)
self.Bind(wx.EVT_BUTTON,self.bu6,self.button_43)
self.Bind(wx.EVT_BUTTON,self.bu_min,self.button_44)
self.Bind(wx.EVT_BUTTON,self.bu7,self.button_46)
self.Bind(wx.EVT_BUTTON,self.bu8,self.button_45)
self.Bind(wx.EVT_BUTTON,self.bu9,self.button_47)
self.Bind(wx.EVT_BUTTON,self.bu_mul,self.button_48)
self.Bind(wx.EVT_BUTTON,self.bu_clear,self.button_49)
self.Bind(wx.EVT_BUTTON,self.bu0,self.button_50)
self.Bind(wx.EVT_BUTTON,self.bu_result,self.button_51)
self.Bind(wx.EVT_BUTTON,self.bu_chu,self.button_52)
#endwxGlade
self.Show(True)
def__set_properties(self):
#beginwxGlade:MyFrame.__set_properties
self.SetTitle("PythonCalculaterbyCYG")
self.text_ctrl_1.SetMinSize((400,50))
self.button_37.SetMinSize((100,50))
self.button_38.SetMinSize((100,50))
self.button_39.SetMinSize((100,50))
self.button_40.SetMinSize((100,50))
self.button_41.SetMinSize((100,50))
self.button_42.SetMinSize((100,50))
self.button_43.SetMinSize((100,50))
self.button_44.SetMinSize((100,50))
self.button_46.SetMinSize((100,50))
self.button_45.SetMinSize((100,50))
self.button_47.SetMinSize((100,50))
self.button_48.SetMinSize((100,50))
self.button_49.SetMinSize((100,50))
self.button_50.SetMinSize((100,50))
self.button_51.SetMinSize((100,50))
self.button_52.SetMinSize((100,50))
#endwxGlade
def__do_layout(self):
#beginwxGlade:MyFrame.__do_layout
sizer_2=wx.BoxSizer(wx.VERTICAL)
sizer_3=wx.BoxSizer(wx.VERTICAL)
grid_sizer_1=wx.GridSizer(4,4,0,0)
sizer_3.Add(self.text_ctrl_1,0,0,0)
grid_sizer_1.Add(self.button_37,0,0,0)
grid_sizer_1.Add(self.button_38,0,0,0)
grid_sizer_1.Add(self.button_39,0,0,0)
grid_sizer_1.Add(self.button_40,0,0,0)
grid_sizer_1.Add(self.button_41,0,0,0)
grid_sizer_1.Add(self.button_42,0,0,0)
grid_sizer_1.Add(self.button_43,0,0,0)
grid_sizer_1.Add(self.button_44,0,0,0)
grid_sizer_1.Add(self.button_46,0,0,0)
grid_sizer_1.Add(self.button_45,0,0,0)
grid_sizer_1.Add(self.button_47,0,0,0)
grid_sizer_1.Add(self.button_48,0,0,0)
grid_sizer_1.Add(self.button_49,0,0,0)
grid_sizer_1.Add(self.button_50,0,0,0)
grid_sizer_1.Add(self.button_51,0,0,0)
grid_sizer_1.Add(self.button_52,0,0,0)
sizer_3.Add(grid_sizer_1,1,wx.EXPAND,0)
sizer_2.Add(sizer_3,1,wx.EXPAND,0)
self.SetSizer(sizer_2)
sizer_2.Fit(self)
self.Layout()
#endwxGlade
defbu1(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("1")
event.Skip()
defbu2(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("2")
event.Skip()
defbu3(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("3")
event.Skip()
defbu_plus(self,event):#wxGlade:MyFrame.<event_handler>
self.num1=self.text_ctrl_1.GetValue()
self.op="+"
self.text_ctrl_1.Clear()
event.Skip()
defbu4(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("4")
event.Skip()
defbu5(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("5")
event.Skip()
defbu6(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("6")
event.Skip()
defbu_min(self,event):#wxGlade:MyFrame.<event_handler>
self.num1=self.text_ctrl_1.GetValue()
self.op="-"
self.text_ctrl_1.Clear()
event.Skip()
defbu7(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("7")
event.Skip()
defbu8(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("8")
event.Skip()
defbu9(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("9")
event.Skip()
defbu_mul(self,event):#wxGlade:MyFrame.<event_handler>
self.num1=self.text_ctrl_1.GetValue()
self.op="x"
self.text_ctrl_1.Clear()
event.Skip()
defbu_clear(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.Clear()
event.Skip()
defbu0(self,event):#wxGlade:MyFrame.<event_handler>
self.text_ctrl_1.AppendText("0")
event.Skip()
defbu_chu(self,event):#wxGlade:MyFrame.<event_handler>
self.num1=self.text_ctrl_1.GetValue()
self.op="/"
self.text_ctrl_1.Clear()
event.Skip()
defbu_result(self,event):#wxGlade:MyFrame.<event_handler>
self.num2=self.text_ctrl_1.GetValue()
num1=int(self.num1)
num2=int(self.num2)
ifself.op=="+":
self.text_ctrl_1.SetValue(str(num1+num2))
elifself.op=="-":
self.text_ctrl_1.SetValue(str(num1-num2))
elifself.op=="x":
self.text_ctrl_1.SetValue(str(num1*num2))
elifself.op=="/":
self.text_ctrl_1.SetValue(str(num1/num2))
event.Skip()
#endofclassMyFrame
if__name__=="__main__":
app=wx.App(False)
myframe=MyFrame(None)
app.MainLoop()

Ⅱ 用PYTHON2做個計算器,只要加減乘除

# encoding: UTF-8
from Tkinter import *
class App:
def __init__(self,master):
frame=Frame(master)
frame.pack()

self.v = StringVar()
self.v.set(" ")
self.entry1=Entry(frame,text="")
self.entry1.grid(row=0,columnspan=2)
self.label0=Label(frame,text="Answer:").grid(row=1,column=0)
self.label1=Label(frame,textvariable=self.v).grid(row=1,column=1)

self.button1=Button(frame,text=" = ",command=self.inputnumber).grid(row=2)

#self.hi_there.pack(side=LEFT)
def inputnumber(self):
self.v.set(eval(self.entry1.get()))

root=Tk()
app=App(root)
root.mainloop()

以上代碼拷貝下來保存為一個py文件,然後運行就好。
注意沒有任何錯誤檢查的。

Ⅲ Python加法簡單計算器

python2里raw_input返回的是字元串啊
所以你的寫法變數ac都是字元串,用+運算也不是數字相加,是字元串相連啊

a = input('請輸入數字a')
b = raw_input('請輸入*+-其中一個運算符')
c = input('請輸入數字b')
if (b=="+"):
sum = a+c
print "%d"% (sum)

熱點內容
手機mc怎麼玩伺服器國際服 發布:2025-01-11 06:18:33 瀏覽:156
win2008ftp中文亂碼 發布:2025-01-11 06:10:03 瀏覽:867
平板配置為什麼這么低 發布:2025-01-11 06:05:30 瀏覽:621
可編程視頻 發布:2025-01-11 06:03:24 瀏覽:785
java多線程編程實戰 發布:2025-01-11 06:03:17 瀏覽:631
圖的演算法java 發布:2025-01-11 05:57:07 瀏覽:482
梯形圖編譯器 發布:2025-01-11 05:56:26 瀏覽:260
安卓framework編譯 發布:2025-01-11 05:55:00 瀏覽:696
加密學原理 發布:2025-01-11 05:54:20 瀏覽:788
ocr編程 發布:2025-01-11 05:51:24 瀏覽:253