Search code examples
pythonwxpythonwxwidgets

Getting IntCtrl and NumCtrl to return value after button(wx.Button) widget is pressed


This is my first gui in wxPython, so I apologize if this comes of as something too elementary to deem a post.

I have a gui that is sending values to a serial controller, after the decimal value is converted to a hex value and sent over as a string. My gui contains a few IntCtrl and NumCtrl inputs. When a user presses a button corresponding to one of the inputs, the box should return whatever value it contains at that moment.

so let's say I have the following NumCtrl & Button widgets:

flt1 = NumCtrl(self, pos, size)
flt1.GetValue()
btn1 = wx.Button(self, label, pos, size)

I seem to be getting confused as to how to bind the button(btn1) press to grab the value in a box(flt1). How do I do?


Solution

  • You need to bind button object with any method so that whenever button get pressed that event get called. for example,

    import wx
    class Example(wx.Frame):
        self.flt1 = NumCtrl(self, pos, size)
        self.btn1 = wx.Button(self, label, pos, size)
        self.btn1.bind(wx.EVT_BUTTON, btn1Click)
    
        def btn1Click(self,event):
            self.flt1.GetValue()