An example of a simple GUI calculator based on Wxpython development

Source: Internet
Author: User
This article describes a simple GUI calculator based on Wxpython development. Share to everyone for your reference. Specific as follows:

# WxCalc1 A simple GUI calculator using wxpython# created with the Boa Constructor which generates all the GUI components# All I had to do are add some code for each button click event# Boa free from:http://boa-constructor.sourceforge.net/# not  E that boa-constructor-0.3.1.win32.exe# still uses wxpythonwin32-2.4.2.4-py23.exe# are 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 (+)) class WxFrame1 (Wxframe): # Startregion, below this marker was Boa generated code do notEdit!!! def _init_ctrls (self, prnt): # generated method, don ' t edit wxframe.__init__ (self, id=wxid_wxframe1, name= ", Parent =prnt, Pos=wxpoint (306, +), Size=wxsize (266, 265), Style=wxdefault_frame_style, title= ' Calculator1 ') self . Setclientsize (wxsize (258, 225)) self. Setbackgroundcolour (wxcolour (0, 0)) Self.btn1 = Wxbutton (id=wxid_wxframe1btn1, label= ' 1 ', name= ' btn1 ', paren T=self, Pos=wxpoint (136), size=wxsize (+, +), 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 (+, +), 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, 13 6), Size=wxsize (+, +), style=0) Evt_button (Self.btn3, wxid_wxframe1btn3, self. Onbtn3button) Self.btn4 = Wxbutton (Id=wxid_wxframe1btn4, label=' 4 ', name= ' btn4 ', parent=self, Pos=wxpoint (+), size=wxsize (+), style=0) Evt_button (Self.btn4, WXID_WXFR Ame1btn4, self. Onbtn4button) Self.btn5 = Wxbutton (id=wxid_wxframe1btn5, label= ' 5 ', name= ' btn5 ', Parent=self, Pos=wxpoint (64, 96) , Size=wxsize (+, +), 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 (+, +), 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 (+, +), 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 (+, +), style=0) Evt_button (SELF.BTN8, wxid_wxframe1btn8, self. Onbtn8button) Self.btn9 = Wxbutton (ID=WXID_WXFRAme1btn9, label= ' 9 ', name= ' btn9 ', Parent=self, Pos=wxpoint (in A.), Size=wxsize (+, +), style=0) Evt_button (sel F.btn9, Wxid_wxframe1btn9, self. Onbtn9button) Self.btn0 = Wxbutton (id=wxid_wxframe1btn0, label= ' 0 ', name= ' btn0 ', Parent=self, Pos=wxpoint (16, 176 ), Size=wxsize (+, +), style=0) Evt_button (Self.btn0, wxid_wxframe1btn0, self. Onbtn0button) Self.btndot = Wxbutton (Id=wxid_wxframe1btndot, label= '. ', name= ' Btndot ', Parent=self, Pos=wxPoint (6 4, 176), size=wxsize (+, +), style=0) Evt_button (Self.btndot, Wxid_wxframe1btndot, self. Onbtndotbutton) self.btnequal = Wxbutton (id=wxid_wxframe1btnequal, label= ' = ', name= ' btnequal ', parent=self, pos=w XPoint (176), size=wxsize (+, +), style=0) Evt_button (self.btnequal, wxid_wxframe1btnequal, self. Onbtnequalbutton) Self.edit = Wxtextctrl (Id=wxid_wxframe1edit, name= ' edit ', parent=self, Pos=wxpoint (+), siz E=wxsize (224), style=0, value= ") Self.btnplus = Wxbutton (id=wxid_Wxframe1btnplus, label= ' + ', name= ' btnplus ', parent=self, Pos=wxpoint (up, up), size=wxsize (+), style=0) Evt_button (Self.btnplus, wxid_wxframe1btnplus, self. Onbtnplusbutton) Self.btnminus = Wxbutton (Id=wxid_wxframe1btnminus, label= '-', name= ' Btnminus ', parent=self, pos= Wxpoint (), size=wxsize (+, +), style=0) Evt_button (Self.btnminus, Wxid_wxframe1btnminus, self. Onbtnminusbutton) Self.btnmulti = Wxbutton (Id=wxid_wxframe1btnmulti, label= ' * ', name= ' Btnmulti ', parent=self, POS =wxpoint (136), size=wxsize (+, +), style=0) Evt_button (Self.btnmulti, Wxid_wxframe1btnmulti, self. Onbtnmultibutton) Self.btndiv = Wxbutton (id=wxid_wxframe1btndiv, label= '/', name= ' Btndiv ', parent=self, Pos=wxPoi NT (176), size=wxsize (+, +), style=0) Evt_button (Self.btndiv, Wxid_wxframe1btndiv, self. Onbtndivbutton) Self.btnclear = Wxbutton (id=wxid_wxframe1btnclear, label= ' C ', name= ' btnclear ', parent=self, pos=w XPoint (208, 56), Size=wxsize (+), style=0) self.btnClear.SetToolTipString (' Btnclear ') Evt_button (Self.btnclear, WXID_WXFR Ame1btnclear, self. Onbtnclearbutton) def __init__ (self, parent): Self._init_ctrls (parent) #endregion, above this marker is Boa generated C Ode, do NOT edit!!! # now respond to all the button click events ... def onbtn0button (self, event): val = ' 0 ' # get existing edit box t   Ext 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) d EF 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.s Etvalue (TXT) def onbtn7button (Self, event): val = ' 7 ' txt = self.edit.GetValue () txt = txt + val self.edit.Se TValue (TXT) def onbtn8button (Self, event): val = ' 8 ' txt = self.edit.GetValue () txt = txt + val self.edit.Set Value (TXT) def onbtn9button (Self, event): val = ' 9 ' txt = self.edit.GetValue () txt = txt + val Self.edit.SetV    Alue (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, Ev  ENT): 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 (SE LF, event): val = ' * ' txt = self.edit.GetValue () txt = txt + val self.edit.SetValue (TXT) def Onbtndivbutton (s Elf, event): val = '/' txt = self.edit.GetValue () txt = txt + val self.edit.SetValue (TXT) def Onbtnclearbutto N (Self, Event): Self.edit.SetValue (") #--------------------End of Class wxFrame1----------------------def create (par ENT): Return wxFrame1 (Parent) class Boaapp (Wxapp): def OnInit (self): wxinitallimagehandlers () Self.main = Create (No NE) self.main.Show () self. Settopwindow (Self.main) return truedef main (): application = Boaapp (0) application. Mainloop () if __name__ = = '__main__ ': Main () 

Hopefully this article will help you with Python programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.