How to retrieve multiple numbers from TextCtrl?

166 views Asked by At

I have following set of TextCtrl which I edit when needed.

import wx
class MyCheckBox(wx.Frame):
   def __init__(self):
       wx.Frame.__init__(self, None, -1, 'Values', size=(490, 450))
       panel = wx.Panel(self, -1)

       List = [(70, 55), (170, 55), (270, 55), (370, 55),
               (70, 85), (170, 85), (270, 85), (370, 85)]

       val = []
       for i in range(0,8):
           value = -999
           val.append(value)

       for p,v in zip(List,val):
           self.value = wx.TextCtrl(panel, -1, value=str(v), pos=p, size=(60,25))

       self.btnOK = wx.Button(panel, label="OK", pos=(190, 355))
       self.Bind(wx.EVT_BUTTON, self.OnOK, id = self.btnOK.GetId())

   def OnOK(self, event):
       self.Show(False)

Currently, default value for all of them is '-999'. How can I retrieve them to a list at the end of process when I press OK?

1

There are 1 answers

0
joaquin On BEST ANSWER

You need to keep your textcontrols inside a list in order to recover their values afterwards:

import wx

class MyCheckBox(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Values', size=(490, 250))
        panel = wx.Panel(self, -1)

        positions = [(70, 55), (170, 55), (270, 55), (370, 55),
                     (70, 85), (170, 85), (270, 85), (370, 85)]

        values = ['a', 'a','a', 'a','a', 'a','a', 'a',]

        self.controls = []        
        for pos, value in zip(positions, values):
           control = wx.TextCtrl(panel, -1, value=str(value),
                                 pos=pos, size=(60,25))
           self.controls.append(control)

        self.to_show = wx.TextCtrl(panel, -1, value='',
                                   pos=(90, 120), size=(290,25))

        self.btnOK = wx.Button(panel, label="OK", pos=(190, 180))
        self.Bind(wx.EVT_BUTTON, self.OnOK, self.btnOK)

    def OnOK(self, event):
        all_values = []
        for control in self.controls:
            all_values.append(control.GetValue())

        self.to_show.SetValue(str(all_values))

if __name__ == '__main__':
    app = wx.PySimpleApp()
    f = MyCheckBox()
    f.Show()
    app.MainLoop()

enter image description here