基于wxpython开发的简单gui计算器实例
本文实例讲述了基于wxpython开发的简单gui计算器。分享给大家供大家参考。具体如下:
#wxCalc1asimpleGUIcalculatorusingwxPython #createdwiththeBoaConstructorwhichgeneratesalltheGUIcomponents #allIhadtodoisaddsomecodeforeachbuttonclickevent #Boafreefrom:http://boa-constructor.sourceforge.net/ #notethatboa-constructor-0.3.1.win32.exe #stilluseswxPythonWIN32-2.4.2.4-Py23.exe #butisexpectedtoworkwithwxPythonversion2.5soon #testedwithPython23vegaseat26feb2005 fromwxPython.wximport* #someBoageneratedglobalIDs... [wxID_WXFRAME1,wxID_WXFRAME1BTN0,wxID_WXFRAME1BTN1,wxID_WXFRAME1BTN2, wxID_WXFRAME1BTN3,wxID_WXFRAME1BTN4,wxID_WXFRAME1BTN5,wxID_WXFRAME1BTN6, wxID_WXFRAME1BTN7,wxID_WXFRAME1BTN8,wxID_WXFRAME1BTN9, wxID_WXFRAME1BTNCLEAR,wxID_WXFRAME1BTNDIV,wxID_WXFRAME1BTNDOT, wxID_WXFRAME1BTNEQUAL,wxID_WXFRAME1BTNMINUS,wxID_WXFRAME1BTNMULTI, wxID_WXFRAME1BTNPLUS,wxID_WXFRAME1EDIT, ]=map(lambda_init_ctrls:wxNewId(),range(19)) classwxFrame1(wxFrame): #startregion,belowthismarkerisBoageneratedcodedonotedit!!! def_init_ctrls(self,prnt): #generatedmethod,don'tedit wxFrame.__init__(self,id=wxID_WXFRAME1,name='',parent=prnt, pos=wxPoint(306,270),size=wxSize(266,265), style=wxDEFAULT_FRAME_STYLE,title='Calculator1') self.SetClientSize(wxSize(258,225)) self.SetBackgroundColour(wxColour(0,128,0)) self.btn1=wxButton(id=wxID_WXFRAME1BTN1,label='1',name='btn1', parent=self,pos=wxPoint(16,136),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn1,wxID_WXFRAME1BTN1,self.OnBtn1Button) self.btn2=wxButton(id=wxID_WXFRAME1BTN2,label='2',name='btn2', parent=self,pos=wxPoint(64,136),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn2,wxID_WXFRAME1BTN2,self.OnBtn2Button) self.btn3=wxButton(id=wxID_WXFRAME1BTN3,label='3',name='btn3', parent=self,pos=wxPoint(112,136),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn3,wxID_WXFRAME1BTN3,self.OnBtn3Button) self.btn4=wxButton(id=wxID_WXFRAME1BTN4,label='4',name='btn4', parent=self,pos=wxPoint(16,96),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn4,wxID_WXFRAME1BTN4,self.OnBtn4Button) self.btn5=wxButton(id=wxID_WXFRAME1BTN5,label='5',name='btn5', parent=self,pos=wxPoint(64,96),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn5,wxID_WXFRAME1BTN5,self.OnBtn5Button) self.btn6=wxButton(id=wxID_WXFRAME1BTN6,label='6',name='btn6', parent=self,pos=wxPoint(112,96),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn6,wxID_WXFRAME1BTN6,self.OnBtn6Button) self.btn7=wxButton(id=wxID_WXFRAME1BTN7,label='7',name='btn7', parent=self,pos=wxPoint(16,56),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn7,wxID_WXFRAME1BTN7,self.OnBtn7Button) self.btn8=wxButton(id=wxID_WXFRAME1BTN8,label='8',name='btn8', parent=self,pos=wxPoint(64,56),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn8,wxID_WXFRAME1BTN8,self.OnBtn8Button) self.btn9=wxButton(id=wxID_WXFRAME1BTN9,label='9',name='btn9', parent=self,pos=wxPoint(112,56),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn9,wxID_WXFRAME1BTN9,self.OnBtn9Button) self.btn0=wxButton(id=wxID_WXFRAME1BTN0,label='0',name='btn0', parent=self,pos=wxPoint(16,176),size=wxSize(32,32),style=0) EVT_BUTTON(self.btn0,wxID_WXFRAME1BTN0,self.OnBtn0Button) self.btnDot=wxButton(id=wxID_WXFRAME1BTNDOT,label='.',name='btnDot', parent=self,pos=wxPoint(64,176),size=wxSize(32,32),style=0) EVT_BUTTON(self.btnDot,wxID_WXFRAME1BTNDOT,self.OnBtnDotButton) self.btnEqual=wxButton(id=wxID_WXFRAME1BTNEQUAL,label='=', name='btnEqual',parent=self,pos=wxPoint(112,176), size=wxSize(32,32),style=0) EVT_BUTTON(self.btnEqual,wxID_WXFRAME1BTNEQUAL,self.OnBtnEqualButton) self.edit=wxTextCtrl(id=wxID_WXFRAME1EDIT,name='edit',parent=self, pos=wxPoint(16,16),size=wxSize(224,24),style=0,value='') self.btnPlus=wxButton(id=wxID_WXFRAME1BTNPLUS,label='+', name='btnPlus',parent=self,pos=wxPoint(160,56),size=wxSize(32, 32),style=0) EVT_BUTTON(self.btnPlus,wxID_WXFRAME1BTNPLUS,self.OnBtnPlusButton) self.btnMinus=wxButton(id=wxID_WXFRAME1BTNMINUS,label='-', name='btnMinus',parent=self,pos=wxPoint(160,96), size=wxSize(32,32),style=0) EVT_BUTTON(self.btnMinus,wxID_WXFRAME1BTNMINUS,self.OnBtnMinusButton) self.btnMulti=wxButton(id=wxID_WXFRAME1BTNMULTI,label='*', name='btnMulti',parent=self,pos=wxPoint(160,136), size=wxSize(32,32),style=0) EVT_BUTTON(self.btnMulti,wxID_WXFRAME1BTNMULTI,self.OnBtnMultiButton) self.btnDiv=wxButton(id=wxID_WXFRAME1BTNDIV,label='/',name='btnDiv', parent=self,pos=wxPoint(160,176),size=wxSize(32,32),style=0) EVT_BUTTON(self.btnDiv,wxID_WXFRAME1BTNDIV,self.OnBtnDivButton) self.btnClear=wxButton(id=wxID_WXFRAME1BTNCLEAR,label='C', name='btnClear',parent=self,pos=wxPoint(208,56), size=wxSize(32,32),style=0) self.btnClear.SetToolTipString('btnClear') EVT_BUTTON(self.btnClear,wxID_WXFRAME1BTNCLEAR,self.OnBtnClearButton) def__init__(self,parent): self._init_ctrls(parent) #endregion,abovethismarkerisBoageneratedcode,donotedit!!! #nowrespondtoallthebuttonclickevents... defOnBtn0Button(self,event): val='0' #getexistingeditboxtext txt=self.edit.GetValue() #appendtext txt=txt+val #updateeditboxtext self.edit.SetValue(txt) defOnBtn1Button(self,event): val='1' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn2Button(self,event): val='2' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn3Button(self,event): val='3' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn4Button(self,event): val='4' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn5Button(self,event): val='5' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn6Button(self,event): val='6' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn7Button(self,event): val='7' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn8Button(self,event): val='8' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtn9Button(self,event): val='9' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtnDotButton(self,event): val='.' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtnEqualButton(self,event): txt=self.edit.GetValue() #needstocontainafloatsoeg.3/5is3/5.0 #otherwisedivision3/5wouldresultinzero if'/'intxt: if'.'notintxt: txt=txt+'.0' #nowevaluatethemathstring txt=repr(eval(txt)) #andshowresultineditbox self.edit.SetValue(txt) defOnBtnPlusButton(self,event): val='+' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtnMinusButton(self,event): val='-' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtnMultiButton(self,event): val='*' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtnDivButton(self,event): val='/' txt=self.edit.GetValue() txt=txt+val self.edit.SetValue(txt) defOnBtnClearButton(self,event): self.edit.SetValue('') #--------------------endofclasswxFrame1---------------------- defcreate(parent): returnwxFrame1(parent) classBoaApp(wxApp): defOnInit(self): wxInitAllImageHandlers() self.main=create(None) self.main.Show() self.SetTopWindow(self.main) returnTrue defmain(): application=BoaApp(0) application.MainLoop() if__name__=='__main__': main()
希望本文所述对大家的Python程序设计有所帮助。