I have some code that I am using to help understand sizers, wx.EXPAND, proportional, etc. The code works, but I am trying to fix the size of some text controls and can’t seem to make it happen. The code is below. The idea is to (1) create the widgets, (2) loop over them finding the largest “best size”, (3) loop over them again, setting the size, best size or whatever should be set on all of them to a good initial value.
It appears that step (2) collects a best size of about (100,21) and it should be more like (160,21), so now what I do in step (3) doesn’t have the desired effect.
How can I find the size of a text control that is needed to contain it’s content?
import wx
class MyFrame(wx.Frame):
def init(self, *args, **kwds):
kwds[“style”] = wx.DEFAULT_FRAME_STYLE
wx.Frame.init(self, *args, **kwds)
panel = wx.Panel(self, -1)
(1) create some controls
tc1 = wx.TextCtrl(panel, -1, “(tc1, 1, wx.ALL|wx.EXPAND, 10)”, name=‘tc1’)
tc2 = wx.TextCtrl(panel, -1, “(tc2, 0, wx.ALL|wx.EXPAND, 10)”, name=‘tc2’)
tc3 = wx.TextCtrl(panel, -1, “(tc3, 1, wx.ALL, 10)”, name=‘tc3’)
tc4 = wx.TextCtrl(panel, -1, “(tc4, 0, wx.ALL, 10)”, name=‘tc4’)
make all widgets same size
widgets = (tc1,tc2,tc3,tc4)
(2) find best size
size = [-1,-1]
for widget in widgets:
chk = widget.GetBestSize() # <- does not get what I want
size[0] = max(size[0], chk[0])
size[1] = max(size[1], chk[1])
(3) apply size to all widgets
for widget in widgets:
widget.SetSize(size)
layout everything
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(tc1, 1, wx.ALL|wx.EXPAND, 10)
sizer.Add(tc2, 0, wx.ALL|wx.EXPAND, 10)
sizer.Add(tc3, 1, wx.ALL, 10)
sizer.Add(tc4, 0, wx.ALL, 10)
panel.SetSizerAndFit(sizer)
sizer2 = wx.BoxSizer(wx.VERTICAL)
sizer2.Add(panel, 1, wx.EXPAND, 0)
self.SetSizerAndFit(sizer2)
self.Layout()
self.Show()
from wx.lib.mixins.inspection import InspectionMixin
class MyApp(wx.App, InspectionMixin):
def OnInit(self):
InspectionMixin.Init(self)
frame = MyFrame(None)
return 1
MyApp(False).MainLoop()