本文实例讲述了基于wxpython开发的简单gui计算器。分享给大家供大家参考。具体如下:
# wxCalc1 a simple GUI calculator using wxPython# created with the Boa Constructor which generates all the GUI components# all I had to do is add some code for each button click event# Boa free from: http://boa-constructor.sourceforge.net/# note that boa-constructor-0.3.1.win32.exe# still uses wxPythonWIN32-2.4.2.4-Py23.exe# but is expected to work with wxPython version 2.5 soon# tested with Python23 vegaseat 26feb2005from wxPython.wx import *# some Boa generated global IDs ...[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))class wxFrame1(wxFrame):#startregion,below this marker is Boa generated code do not edit!!! def _init_ctrls(self,prnt): # generated method,don't edit 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,self.OnBtn1button) self.btn2 = wxbutton(ID=wxID_WXFRAME1BTN2,label='2',name='btn2',pos=wxPoint(64,style=0) EVT_button(self.btn2,self.OnBtn2button) self.btn3 = wxbutton(ID=wxID_WXFRAME1BTN3,label='3',name='btn3',pos=wxPoint(112,style=0) EVT_button(self.btn3,self.OnBtn3button) self.btn4 = wxbutton(ID=wxID_WXFRAME1BTN4,label='4',name='btn4',96),style=0) EVT_button(self.btn4,self.OnBtn4button) self.btn5 = wxbutton(ID=wxID_WXFRAME1BTN5,label='5',name='btn5',style=0) EVT_button(self.btn5,self.OnBtn5button) self.btn6 = wxbutton(ID=wxID_WXFRAME1BTN6,label='6',name='btn6',style=0) EVT_button(self.btn6,self.OnBtn6button) self.btn7 = wxbutton(ID=wxID_WXFRAME1BTN7,label='7',name='btn7',56),style=0) EVT_button(self.btn7,self.OnBtn7button) self.btn8 = wxbutton(ID=wxID_WXFRAME1BTN8,label='8',name='btn8',style=0) EVT_button(self.btn8,self.OnBtn8button) self.btn9 = wxbutton(ID=wxID_WXFRAME1BTN9,label='9',name='btn9',style=0) EVT_button(self.btn9,self.OnBtn9button) self.btn0 = wxbutton(ID=wxID_WXFRAME1BTN0,label='0',name='btn0',176),style=0) EVT_button(self.btn0,self.OnBtn0button) self.btnDot = wxbutton(ID=wxID_WXFRAME1BTNDOT,label='.',name='btnDot',style=0) EVT_button(self.btnDot,self.OnBtnDotbutton) self.btnEqual = wxbutton(ID=wxID_WXFRAME1BTNEQUAL,label='=',name='btnEqual',style=0) EVT_button(self.btnEqual,self.OnBtnEqualbutton) self.edit = wxTextCtrl(ID=wxID_WXFRAME1EDIT,name='edit',16),size=wxSize(224,24),style=0,value='') self.btnPlus = wxbutton(ID=wxID_WXFRAME1BTNPLUS,label='+',name='btnPlus',pos=wxPoint(160,style=0) EVT_button(self.btnPlus,self.OnBtnPlusbutton) self.btnMinus = wxbutton(ID=wxID_WXFRAME1BTNMINUS,label='-',name='btnMinus',style=0) EVT_button(self.btnMinus,self.OnBtnMinusbutton) self.btnMulti = wxbutton(ID=wxID_WXFRAME1BTNMulTI,label='*',name='btnMulti',style=0) EVT_button(self.btnMulti,self.OnBtnMultibutton) self.btndiv = wxbutton(ID=wxID_WXFRAME1BTNdiv,label='/',name='btndiv',style=0) EVT_button(self.btndiv,self.OnBtndivbutton) self.btnClear = wxbutton(ID=wxID_WXFRAME1BTNCLEAR,label='C',name='btnClear',pos=wxPoint(208,style=0) self.btnClear.SettooltipString('btnClear') EVT_button(self.btnClear,self.OnBtnClearbutton) def __init__(self,parent): self._init_ctrls(parent)#endregion,above this marker is Boa generated code,do not edit!!!# Now respond to all the button click events ... def OnBtn0button(self,event): val = '0' # get existing edit Box text txt = self.edit.GetValue() # append text txt = txt + val # update edit Box text self.edit.SetValue(txt) def OnBtn1button(self,event): val = '1' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn2button(self,event): val = '2' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn3button(self,event): val = '3' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn4button(self,event): val = '4' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn5button(self,event): val = '5' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn6button(self,event): val = '6' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn7button(self,event): val = '7' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn8button(self,event): val = '8' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtn9button(self,event): val = '9' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtnDotbutton(self,event): val = '.' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtnEqualbutton(self,event): txt = self.edit.GetValue() # needs to contain a float so eg. 3/5 is 3/5.0 # otherwise division 3/5 would result in zero if '/' in txt: if '.' not in txt: txt = txt + '.0' # Now evaluate the math string txt = repr(eval(txt)) # and show result in edit Box self.edit.SetValue(txt) def OnBtnPlusbutton(self,event): val = '+' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtnMinusbutton(self,event): val = '-' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtnMultibutton(self,event): val = '*' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtndivbutton(self,event): val = '/' txt = self.edit.GetValue() txt = txt + val self.edit.SetValue(txt) def OnBtnClearbutton(self,event): self.edit.SetValue('')# -------------------- end of class wxFrame1 ----------------------def create(parent): return wxFrame1(parent)class BoaApp(wxApp): def OnInit(self): wxInitAllimageHandlers() self.main = create(None) self.main.Show() self.SettopWindow(self.main) return Truedef main(): application = BoaApp(0) application.MainLoop()if __name__ == '__main__': main()
希望本文所述对大家的Python程序设计有所帮助。
总结以上是内存溢出为你收集整理的基于wxpython开发的简单gui计算器实例全部内容,希望文章能够帮你解决基于wxpython开发的简单gui计算器实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)