Hi!
I’m writing an app that uses a CheckListBox that I fill filenames after it’s created. I’m using the InsertObjects() method to insert the items in an otherwise empty control. However, there is a humongous amount of whitespaced to the left of each item. I broke the example down to the following code which for reasons I’m not understanding, works correctly:
import os
import glob
import wx
class MyPanel(wx.Panel):
source_directory = 'data'
def __init__(self, parent):
super().__init__(parent)
self.main_sizer = wx.BoxSizer();
self.left_panel_sizer = wx.BoxSizer(wx.VERTICAL);
self.right_panel_sizer = wx.BoxSizer(wx.VERTICAL);
lbl = wx.StaticText(self, label='CheckListBox Test')
self.left_panel_sizer.Add(lbl, 0, wx.ALL, 5)
self.clb = wx.CheckListBox(self, size=(300, 200))
self.left_panel_sizer.Add(self.clb, 0, wx.ALL, 5)
self.main_sizer.Add(self.left_panel_sizer, 0, wx.ALL, 5)
self.load_btn = wx.Button(self, label='Load')
self.load_btn.Bind(wx.EVT_BUTTON, self.load_files)
self.right_panel_sizer.Add(self.load_btn, 0, wx.ALL, 5)
self.main_sizer.Add(self.right_panel_sizer, 0, wx.ALL, 5)
self.SetSizer(self.main_sizer)
def load_files(self, event):
mask = os.path.join(self.source_directory, "*.xlsx")
input_files = []
for f in glob.glob(mask):
input_files.append(os.path.split(f)[1])
self.clb.InsertItems(input_files, 0)
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='CheckListBox Test')
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()
The code above produces the following when the Load button is clicked: