Search code examples
pythonwxpythonwxwidgets

Get value from dynamically created wx.checkbox


This code (thanks Mike Driscoll) creates a grid of 168 checkboxes, one for every hour of everyday. I am stuck when it comes to retrieving their values... I would like to get a dict like {mon1 : yes} etc. Any help appreciated...

import wx

class MainPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        main_sizer = wx.BoxSizer(wx.VERTICAL)

        for row in range(7):
            row_sizer = wx.BoxSizer(wx.HORIZONTAL)
            for col in range(24):
                name_of_checkbox = "row_{row}_col_{col}".format(row=row, col=col)
                checkbox = wx.CheckBox(self, name=name_of_checkbox)
                row_sizer.Add(checkbox, 0, wx.ALL, 5)
            main_sizer.Add(row_sizer)

        self.SetSizer(main_sizer)


class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Hours and days', size=(650, 400))
        panel = MainPanel(self)
        self.Show()


if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
app.MainLoop()

Solution

  • You can create a structure to hold the check box id's and add a button to retrieve all of the data at once. This makes it easy to access which boxes have been checked and which haven't. This way you can retrieve the data in any structure you like i.e. something like this:

    import wx
    
    class MainPanel(wx.Panel):
    
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
    
            main_sizer = wx.BoxSizer(wx.VERTICAL)
            days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
            self.boxes=[]
            for row in range(7):
                row_sizer = wx.BoxSizer(wx.HORIZONTAL)
                day = days[row]
                for col in range(24):
                    name_of_checkbox = "{day}_{hour}".format(day=day, hour=col)
                    self.checkbox = wx.CheckBox(self, name=name_of_checkbox)
                    row_sizer.Add(self.checkbox, 0, wx.ALL, 5)
                    self.boxes.append(self.checkbox)
                main_sizer.Add(row_sizer)
            button = wx.Button(self,-1,"Retrieve Data")
            main_sizer.Add(button)
            self.Bind(wx.EVT_CHECKBOX, self.OnChecked)
            self.Bind(wx.EVT_BUTTON, self.OnGetData)
            self.SetSizer(main_sizer)
    
        def OnChecked(self,event):
            clicked = event.GetEventObject()
            print clicked.GetName()
            print event.IsChecked()
    
        def OnGetData(self,event):
            day_dict = {}
            day_list = []
            for i in self.boxes:
                if i.IsChecked():
                    n = i.GetName()
                    day_dict[n]="Checked"
                    day_list.append((n,"Checked"))
            print day_dict
            print day_list
    
    class MainFrame(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, title='Hours and days', size=(850, 400))
            panel = MainPanel(self)
            self.Show()
    
    
    if __name__ == '__main__':
        app = wx.App()
        frame = MainFrame()
    app.MainLoop()