[wxPython] Getting values of controls after pressing a button ?

Hello,

I just want to write my first Python program with a wxPython GUI and I would
like to know if it is possible to get the values of other controls when a
wxButton is pressed ? I have one wxTextCtrl, one wxChoice and three
wxSpinCtrl control from which I want to get the values.

Bye,
Henning

Hi Henning,

Yes, this is possible. Each control has a method to get its value,
usually "myControl.GetValue()", but in the case of wxChoice it will
be "myChoice.GetSelection()". Use these method calls in a function
that you define. (This function will require an 'event' argument).
Then connect this function to the button using the EVT_BUTTON(self,
id=10, myFunction) macro.

···

--- Henning Sauer <hsauer@henning-sauer.de> wrote:

Hello,

I just want to write my first Python program with a wxPython
GUI and I would like to know if it is possible to get the values
of other controls when a wxButton is pressed ? I have one
wxTextCtrl, one wxChoice and three wxSpinCtrl control from which
I want to get the values.

#############################################################
from wxPython.wx import *

class MyFrame(wxFrame):

    def __init__(self, parent=None, id=-1, title='Get Values'):
        wxFrame.__init__(self, parent, id, title)
        panel = wxPanel(self, -1)
        self.b1 = wxButton(panel, 10, 'OK')
        self.b2 = wxButton(panel, 20, 'Cancel')
        self.t1 = wxTextCtrl(panel, 30, '')
        myChoices = ['Monday', 'Tuesday']
        self.c1 = wxChoice(panel, 40, choices = myChoices)
        self.c1.SetSelection(0)
        self.s1 = wxSpinCtrl(panel, 50, '0')
        self.s2 = wxSpinCtrl(panel, 60, '0')
        self.s3 = wxSpinCtrl(panel, 70, '0')
        EVT_BUTTON(self, 10, self.OnOK)
        EVT_BUTTON(self, 20, self.OnCancel)
        box0 = wxBoxSizer(wxVERTICAL)
        box1 = wxBoxSizer(wxHORIZONTAL)
        box2 = wxBoxSizer(wxHORIZONTAL)
        box1.Add(self.s1, 1, wxEXPAND)
        box1.Add(self.s2, 1, wxEXPAND)
        box1.Add(self.s3, 1, wxEXPAND)
        box2.Add(self.b1, 1, wxEXPAND)
        box2.Add(self.b2, 1, wxEXPAND)
        box0.Add(self.t1, 0, wxEXPAND)
        box0.Add(self.c1, 0, wxEXPAND)
        box0.Add(box1, 1, wxEXPAND)
        box0.Add(box2, 1, wxEXPAND)
        panel.SetSizer(box0)
        panel.Layout()
        panel.SetAutoLayout(true)
        box0.Fit(self)
        box0.SetSizeHints(self)

    def OnOK(self, event):
        print self.t1.GetValue()
        print self.c1.GetSelection()
        print self.c1.GetString(self.c1.GetSelection())
        print self.s1.GetValue()
        print self.s2.GetValue()
        print self.s3.GetValue()

    def OnCancel(self, event):
        self.Close()

class MyApp(wxApp):
    def OnInit(self):
        frame = MyFrame()
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = MyApp(0)
app.MainLoop()

=====
Donnal Walter
Arkansas Children's Hospital

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

Henning,

Here is the example revised with some comments and a RESET button
as well as OK. Cheers.

···

##################################################################
from wxPython.wx import *

class MyFrame(wxFrame):

    def __init__(self, parent=None, id=-1, title='Get Values'):
        wxFrame.__init__(self, parent, id, title)
        panel = wxPanel(self, -1)
        # note the button id's: 10 and 20
        self.b1 = wxButton(panel, 10, 'OK')
        self.b2 = wxButton(panel, 20, 'Reset')
        self.t1 = wxTextCtrl(panel, 30, '')
        myChoices = ['Monday', 'Tuesday']
        self.c1 = wxChoice(panel, 40, choices = myChoices)
        self.c1.SetSelection(0)
        self.s1 = wxSpinCtrl(panel, 50, '0')
        self.s2 = wxSpinCtrl(panel, 60, '0')
        self.s3 = wxSpinCtrl(panel, 70, '0')
        
        # these macros connect the buttons to the functions
        # note the button id's 10 and 20
        EVT_BUTTON(self, 10, self.OnOK)
        EVT_BUTTON(self, 20, self.OnReset)
        
        # the rest of this is to control the frame layout
        box0 = wxBoxSizer(wxVERTICAL)
        box1 = wxBoxSizer(wxHORIZONTAL)
        box2 = wxBoxSizer(wxHORIZONTAL)
        box1.Add(self.s1, 1, wxEXPAND)
        box1.Add(self.s2, 1, wxEXPAND)
        box1.Add(self.s3, 1, wxEXPAND)
        box2.Add(self.b1, 1, wxEXPAND)
        box2.Add(self.b2, 1, wxEXPAND)
        box0.Add(self.t1, 0, wxEXPAND)
        box0.Add(self.c1, 0, wxEXPAND)
        box0.Add(box1, 1, wxEXPAND)
        box0.Add(box2, 1, wxEXPAND)
        panel.SetSizer(box0)
        panel.Layout()
        panel.SetAutoLayout(true)
        box0.Fit(self)
        box0.SetSizeHints(self)

    # this is the function that gets the values
    # notice 'event' is an argument
    def OnOK(self, event):
        print self.t1.GetValue()
        print self.c1.GetSelection()
        print self.c1.GetString(self.c1.GetSelection())
        print self.s1.GetValue()
        print self.s2.GetValue()
        print self.s3.GetValue()
        # if you want to use the event argument
        print event.GetId()

    # this function will reset the values
    def OnReset(self, event):
        self.t1.SetValue('')
        self.c1.SetSelection(0)
        self.s1.SetValue(0)
        self.s2.SetValue(0)
        self.s3.SetValue(0)
        # if you want to use the event argument
        print event.GetId()

class MyApp(wxApp):
    def OnInit(self):
        frame = MyFrame()
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = MyApp(0) # send output to console
app.MainLoop()

=====
Donnal Walter
Arkansas Children's Hospital

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/