Thank you Mike Driscoll and C M for answering my previous mail, my prevous approach was not good. I have now learnt that sizers are smart!
Still, there is one issue left, and that is related to the access of a CheckListBox in class LeftPanel from RightPanel. Below is a runnable sample. Honestly, I have tried to remove all unnecessary pieces of code... See my comment under the OnAdd method in class RightPanel - how do I add a word to the CheckListBox in class LeftPanel.
I have not yet looked into the pubsub as suggested by Mike, hopefully there are a simpler solution...
import wx
class RightPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour('White')
self.addBox = wx.BoxSizer(wx.HORIZONTAL)
self.addText = wx.TextCtrl(self, -1, value='', size = (125, -1), \
validator=wx.DefaultValidator)
self.addText.SetMaxLength(12)
self.addButton = wx.Button(self, -1, 'Add')
self.addBox.Add(self.addText, 0, wx.ALL, 1)
self.addBox.Add(self.addButton, 0, wx.ALL, 1)
self.Bind(wx.EVT_BUTTON, self.OnAdd, self.addButton)
self.addBox.Fit(self)
self.SetSizer(self.addBox)
self.Layout()
def OnAdd(self, event):
word = self.addText.GetValue()
#HERE IS THE CLUE: HOW DO I APPEND word TO THE CheckListBox OF
#THE LEFT PANEL, as I do in the OnAdd method of the LeftPanel class
#This provides a new CheckListBox on top of the previous, and that is NOT what I want
LeftPanel(wx.GetApp().GetTopWindow()).clBox.Append(word)
class LeftPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
#Initialization
self.SetBackgroundColour('White')
self.aList = []
#Boxes
self.mainBox = wx.BoxSizer(wx.VERTICAL)
self.clBox = wx.CheckListBox(self, -1, pos=wx.DefaultPosition,\
size=wx.DefaultSize, choices=self.aList,\
style=0, validator=wx.DefaultValidator,\
name='checklistBox')
#Add box containing textctrl and add button
self.addBox = wx.BoxSizer(wx.HORIZONTAL)
self.addText = wx.TextCtrl(self, -1, value='', size = (125, -1), \
validator=wx.DefaultValidator)
self.addText.SetMaxLength(12)
self.addButton = wx.Button(self, -1, 'Add')
self.addBox.Add(self.addText, 0, wx.ALL, 1)
self.addBox.Add(self.addButton, 0, wx.ALL, 1)
self.Bind(wx.EVT_BUTTON, self.OnAdd, self.addButton)
#Adding to mainbox
self.mainBox.Add(self.clBox, 0, wx.ALL, 1)
self.mainBox.Add(self.addBox, 0, wx.EXPAND, 0)
self.mainBox.Fit(self)
self.SetSizer(self.mainBox)
self.Layout()
def OnAdd(self, event):
word = self.addText.GetValue()
self.clBox.Append(word)
self.Layout()
class MainFrame(wx.Frame):
def __init__(self, parent=None, id=-1, pos=wx.DefaultPosition, \
title='Program name here'):
wx.Frame.__init__(self, parent, id, title, pos, (1100, 600))
#Creating splitter with two panels
self.initpos = 245
self.splitter = wx.SplitterWindow(self)
self.leftPanel = LeftPanel(self.splitter)
self.rightPanel = RightPanel(self.splitter)
self.splitter.Initialize(self.leftPanel)
self.splitter.Initialize(self.rightPanel)
self.splitter.SplitVertically(self.leftPanel, self.rightPanel, self.initpos)
self.leftPanel.Fit()
self.rightPanel.Fit()
self.Centre()
self.Layout()
class App(wx.App):
def OnInit(self):
self.frame = MainFrame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
app = App()
import wx.lib.inspection
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
if __name__ == '__main__':
main()