当前位置:首页 » 编程语言 » 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)

热点内容
win2008ftp中文乱码 发布:2025-01-11 06:10:03 浏览:867
平板配置为什么这么低 发布:2025-01-11 06:05:30 浏览:620
可编程视频 发布:2025-01-11 06:03:24 浏览:784
java多线程编程实战 发布:2025-01-11 06:03:17 浏览:630
图的算法java 发布:2025-01-11 05:57:07 浏览:481
梯形图编译器 发布:2025-01-11 05:56:26 浏览:260
安卓framework编译 发布:2025-01-11 05:55:00 浏览:695
加密学原理 发布:2025-01-11 05:54:20 浏览:788
ocr编程 发布:2025-01-11 05:51:24 浏览:253
androiddecoder 发布:2025-01-11 05:44:13 浏览:732