python計算器
㈠ 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()
㈡ 將 Python 當做計算器中的列表
3.1.3. 列表
Python 有幾個 復合 數據類型,用於表示其它的值。最通用的是 list (列表) ,它可以寫作中括弧之間的一列逗號分隔的值。列表的元素不必是同一類型:
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
就像字元串(以及其它所有內建的 序列 類型)一樣,列表可以被索引和切片:
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
所有的切片操作都會返回一個包含請求的元素的新列表。這意味著下面的切片操作返回列表一個新的(淺)拷貝副本:
>>> squares[:]
[1, 4, 9, 16, 25]
列表也支持連接這樣的操作:
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
不像 不可變的 字元串,列表是 可變的,它允許修改元素:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
你還可以使用 append() 方法 (後面我們會看到更多關於列表的方法的內容)在列表的末尾添加新的元素:
㈢ python編寫計算器
其實有個函數叫eval()的,至於怎麼用,看你的了。
㈣ 用PYTHON2做個計算器,哪位高手能用Python寫出計算器的代碼,需要加減乘除和退出,謝謝!
粗略的寫了一個,沒有包含對輸入的檢查。
#coding:utf-8
'''
計算器
'''
operator={
'a':['aforaddition','+'],
'b':['bforsubtraction','-'],
'c':['cformultiplication','*'],
'd':['dfordivision','/'],
'z':['zforexit',None]
}
defDisplayOperationMenu():
forkey,datainoperator.items():
printdata[0]
defGetInputs():
input_data=raw_input('Inputtwonumbers(format->number1,number2):')
return(int(input_data.split(',')[0]),int(input_data.split(',')[1]))
defGetOperator():
input_operator=raw_input('Selectoneoftheaboveoperates:')
try:
returnoperator[input_operator][1]
except:
return'SKIP'
defCalculator(parameters,operator):
strline='{0}{2}{1}'.format(parameters[0],parameters[1],operator)
try:
print'{}={}'.format(strline,eval(strline))
except:
print'<Incorrectoperation>'
whileTrue:
DisplayOperationMenu()
p=GetOperator()
ifp=='SKIP':
print'Incorrectselection!'
continue
elifpisNone:
break
else:
Calculator(GetInputs(),p)
測試:
運行的時候選擇d
輸入100,20
㈤ python計算器怎麼循環
python 中的循環分三種:
while 循環
a=0
b=0
whilea<5:
b+=a#相當於b=b+a
print(b)
a+=1
for 循環
b=0
foriinrange(5):
b+=i
print(b)
遞歸循環
b=0
deff(a):
ifa<5:
b+=a
print(b)
f(a+1)
else:
return
f(0)
㈥ Windows科學計算器與Python,計算結果的巨大差異
您這個表達式是從右向左計算的,所以出現了2^2=4---->2^4=16----->2^16=65536,在計算器上計算是從左向右計算的,所以為2^2=4---->4^2=16------>16^2=256
㈦ 求 用python編計算器 原代碼
#!/usr/bin/python
#~替換成TAB或空格
if __name__=="__main__":
~line = None
~while line != 'exit':
~~line = raw_input('>')
~~~try:
~~~~print eval(line)
~~~except:
~~~~print 'Error'
㈧ 用python操作Windows的計算器。
pyhook可以抓到鍵。
不過這里只需要消息就可以了,win32api中的windows的message
使用vc中的消息 工具,抓鍵盤消息。然後再把這個消息包裝一下用pywin32中的API發送過去。
我以前用pyhook加pywin32, 控制過一個游戲,做外掛。
也用pywin32的com介面控制過excel
㈨ python計算器製作
最後一句改為
print("您好!此次包裹寄件費用為{}元,歡迎下次光臨!".format(p))
㈩ 用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文件,然後運行就好。
注意沒有任何錯誤檢查的。