I’ve encountered a phenomenon where a combobox increases the width of gridbagsizer cell it is in when the combobox contains a long string item. For example, for the code at the bottom of the post, when I launch the app, I get a layout like the one in the screenshot below, where the widths of the widgets are evenly distributed. At this point the comboboxes do not contain any items.
If I first click BUTTON1 to append items to the comboboxes (the combobox on the left gets a very long string) , and then click BUTTON2, the layout changes and turns into the one in the screenshot below:
I wonder if there is a way to make sure that the layout does not turn into the one in the 2nd screenshot and that the gridbagsizer’s cells maintain evenly distributed widths. Thanks!
import wx
class Example(wx.Frame):
def __init__(self, parent):
super(Example, self).__init__(parent, size=(500, 400), style=wx.SIMPLE_BORDER|wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
GridBagSizer = wx.GridBagSizer()
self.SetBackgroundColour('white')
self.combobox1 = wx.ComboBox(self, 1, choices = [], size = (-1, -1), style = wx.CB_READONLY)
self.combobox2 = wx.ComboBox(self, 2, choices = [], size = (-1, -1), style = wx.CB_READONLY)
for i in range(4):
GridBagSizer.Add(wx.StaticText(self, -1, label = "label" + str(i), size = (-1, -1), style = wx.ALIGN_CENTER),
pos = (1, i), span = (1, 1), flag = wx.EXPAND, border = 10)
self.statictext1 = wx.StaticText(self, -1, label= "", size = (-1, -1), style = wx.ALIGN_CENTER)
self.statictext2 = wx.StaticText(self, -1, label= "", size = (-1, -1), style = wx.ALIGN_CENTER)
self.statictext3 = wx.StaticText(self, -1, label= "", size = (-1, -1), style = wx.ALIGN_CENTER)
self.statictext4 = wx.StaticText(self, -1, label= "", size = (-1, -1), style = wx.ALIGN_CENTER)
self.statictext1.SetBackgroundColour("gray")
self.statictext2.SetBackgroundColour("gray")
self.statictext3.SetBackgroundColour("gray")
self.statictext4.SetBackgroundColour("gray")
self.Button1 = wx.Button(self, label = "BUTTON1", size = (-1, -1))
self.Button2 = wx.Button(self, label = "BUTTON2", size = (-1, -1))
self.Button1.Bind(wx.EVT_BUTTON, self.OnSelect1)
self.Button2.Bind(wx.EVT_BUTTON, self.OnSelect2)
GridBagSizer.Add(self.combobox1, pos = (0, 0), span = (1, 2), flag = wx.EXPAND|wx.ALL, border = 10)
GridBagSizer.Add(self.combobox2, pos = (0, 2), span = (1, 2), flag = wx.EXPAND|wx.ALL, border = 10)
GridBagSizer.Add(self.statictext1, pos = (2, 0), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
GridBagSizer.Add(self.statictext2, pos = (2, 1), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
GridBagSizer.Add(self.statictext3, pos = (2, 2), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
GridBagSizer.Add(self.statictext4, pos = (2, 3), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
GridBagSizer.Add(self.Button1, pos = (3, 0), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
GridBagSizer.Add(self.Button2, pos = (4, 0), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 10)
for i in range(4):
GridBagSizer.AddGrowableCol(i, 1)
self.SetSizer(GridBagSizer)
self.Layout()
self.Refresh()
def OnSelect1(self, evt):
self.combobox1.Append("short title")
self.combobox1.Append("loooooooooooooooooooooooooooooooooooooooooooong title")
def OnSelect2(self, evt):
self.statictext1.SetLabel("haha")
self.Layout()
def main():
ex = wx.App()
MainFrame = Example(None)
MainFrame.Show()
ex.MainLoop()
if name == ‘main’:
main()
``