newbie wxpython GUI question

Cliff Wells LogiplexSoftware at earthlink.net
Thu Mar 27 15:04:54 EST 2003


On Thu, 2003-03-27 at 11:00, anthony wrote:
> I feel quite lame for not being able to figure this out.  I'm using
> python 2.2, wxpython 2.4.  I want to build a simple GUI where there
> are three text boxes and one button.  I want the user to put values
> into the text boexes and press the button.  When you press the button
> I want to read the data from the text boxes (currently wxTextCtrl) and
> add them up.  I have the event set up for something to happen on the
> button press, but how do I either pass the text box IDs into the
> button event or get the text from the button event?  If this is
> incorrect Python mentality feel free to yell at me.

First, you should be aware that there is a wxPython mailing list that
will be much more responsive than c.l.py.  You can subscribe to it at
www.wxpython.org.

That aside, does this do what you want?

from wxPython.wx import *

class MyWin(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self, parent, id, title)
        self.SetAutoLayout(true)
        sizer = wxBoxSizer(wxVERTICAL)
        self.SetSizer(sizer)
        
        self.ctrls = []
        for ctrl in range(3): # create 3 text controls in a list
            self.ctrls.append(wxTextCtrl(self, -1, ""))
            sizer.Add(self.ctrls[ctrl], 0, wxALIGN_CENTER | wxALL, 2)
            
        self.sum = wxStaticText(self, -1, "0")
        
        add = wxButton(self, -1, "Add")
        
        sizer.AddMany([
            (self.sum, 0, wxALIGN_CENTER | wxALL, 2),
            (add, 0, wxALIGN_CENTER | wxALL, 2),
        ])
        
        EVT_BUTTON(self, add.GetId(), self.OnAdd)
        
        self.Fit()
        
    def OnAdd(self, event):
        sum = 0
        for ctrl in self.ctrls:
            try:
                sum += int(ctrl.GetValue())
            except ValueError:
                continue
                
        self.sum.SetLabel(str(sum))
        self.sum.Update()
        

class MyApp(wxApp):
    def OnInit(self):
        w = MyWin(None, -1, "Test")
        w.Show(true)
        return true
        
app = MyApp()
app.MainLoop()



AND HERE'S YOUR YELLING! <wink>


Regards,

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308






More information about the Python-list mailing list