Appreciate the replies and Dietmar and Tim … I sure wish the gridsizers had an add widget method something like I have now in my Slot class, seems like an obvious thing to have.
So this is what I came up with - it kind of works, still some funnies, and I am refining it, but I will appreciate any suggestions for improvement as I have only been learning python a few weeks (but I am an old software hand in other languages)
The slot panel class
__class Slot_Panel(wx.Panel):
__Col_Max = 0
__Row_Max = 0
__Parent_Window = None
__Sizer = None
def __init__(self,parent, max_slot_rows = 1, max_slot_cols = 1):
Row = 0
Col = 0
Spacer = None
#assert isinstance(parent, ??),"parent must be wx.Frame" #TODO What is the object type of a wx.Frame?
assert isinstance(max_slot_rows, int) and max_slot_rows,"max_slot_rows must be an integer > 0"
assert isinstance(max_slot_cols, int) and max_slot_cols,"max_slot_cols must be an integer > 0"
super().__init__(parent = parent)
self.__Parent_Window = parent
self.__Sizer = wx.GridBagSizer(max_slot_rows, max_slot_cols)
self.__Col_Max = max_slot_cols
self.__Row_Max = max_slot_rows
for Row in range (1,max_slot_rows + 1):
for Col in range (1,max_slot_cols + 1):
#for debug - write out slot number
Spacer = wx.StaticText(self, id = wx.ID_ANY, label = "Control Slot "+str(Row * Col))
#Spacer = wx.StaticText(self, -1, "")
self.__Sizer.Add(Spacer, pos = (Row,Col), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 5)
def Add_Control(self, new_widget, slot_row =1, slot_col = 0):
#assert isinstance(new_widget, ??),"new_widget must be wx.Control" #TODO What is the object type of a wx.control?
assert isinstance(slot_row, int) and slot_row > 0 and slot_row <= self.__Row_Max,"slot row must be an integer > 0 and <= max_slot_rows"
assert isinstance(slot_col, int) and slot_col > 0 and slot_col <= self.__Col_Max,"slot col must be an integer > 0 and <= max_slot_cols"
Old_Widget = self.__Sizer.FindItemAtPosition(wx.GBPosition(slot_row,slot_col))
self.__Sizer.Detach(Old_Widget.GetWindow())
self.__Sizer.Add(new_widget, pos = (slot_row,slot_col), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 5)
self.__Sizer.Layout()__
An MDI Child using this class
**class MDI_Child(wx.MDIChildFrame):
Win_Id = ["",0]
def __init__(self,frame,id=-1,title = ""):
super().__init__(frame,id,title)
Control_Panel = Slot_Panel(self,max_slot_rows = 8, max_slot_cols = 8 )
Button_Panel = Slot_Panel(self,max_slot_rows = 1, max_slot_cols = 8 )
Button_Panel.Add_Control(wx.Button(Button_Panel,label = "Ok"),slot_row = 1, slot_col = 6)
Button_Panel.Add_Control(wx.Button(Button_Panel,label = "Cancel"),slot_row = 1, slot_col = 7)
Button_Panel.Add_Control(wx.Button(Button_Panel,label = "Append"),slot_row = 1, slot_col = 8)
Control_Panel.SetBackgroundColour('light blue')
Button_Panel.SetBackgroundColour('blue')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(Control_Panel, 1, wx.EXPAND)
sizer.Add(Button_Panel, 1, wx.EXPAND)
self.SetSizer(sizer)**
In the program this looks like this -