基于wxpython开发的简单gui计算器实例

本文实例讲述了基于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 26feb2005
from 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_wxframe1btnp, 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, ‘, parent=prnt,
pos=wxpoint(306, 270), size=wxsize(266, 265),
calculator1′)
self.setclientsize(wxsize(258, 225))
self.setbackgroundcolour(wxcolour(0, 128, 0))
self.btn1 = wxbutton(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(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(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(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(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(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(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(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(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(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(.’, 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(=’,
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(edit’, parent=self,
pos=wxpoint(16, 16), size=wxsize(224, 24), ‘)
self.btnplus = wxbutton(+’,
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(-‘,
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(*’,
name=’btnmulti’, parent=self, pos=wxpoint(160, 136),
size=wxsize(32, 32), style=0)
evt_button(self.btnmulti, wxid_wxframe1btnmulti, self.onbtnmultibutton)
self.btnp = wxbutton(/’, name=’btnp’,
parent=self, pos=wxpoint(160, 176), size=wxsize(32, 32), style=0)
evt_button(self.btnp, wxid_wxframe1btnp, self.onbtnpbutton)
self.btnclear = wxbutton(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, 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 pision 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 onbtnpbutton(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 true
def main():
application = boaapp(0)
application.mainloop()
if __name__ == ‘__main__’:
main()

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类