Search code examples
pythonwxpythonsizer

Can't update StaticText in FlexGridSizer or fit it in a Dialog


I'm trying to create a custom Dialog that has user and password TextCtrls that get hidden and replaced with some account information in a FlexGridSizer. There is a vertical BoxSizer that holds a Panel and some Buttons. The Panel itself has a vertical BoxSizer filled with a couple of horizontal BoxSizers and the FlexGridSizer.

self.panel = wx.Panel(self)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.infoBox = wx.BoxSizer(wx.VERTICAL)

And put together like this:

self.panel.SetSizer(self.infoBox)
self.vbox.Add(self.panel, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
self.vbox.Add(self.butBox,
                  flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)
self.SetSizer(self.vbox)

The FlexGridSizer looks like this:

self.acctBox = wx.FlexGridSizer(5, 2, 5, 5)
self.userName = wx.StaticText(self.panel, label='Username:')
self.userNameData = wx.StaticText(self.panel)
self.acctType = wx.StaticText(self.panel, label='Account Type:')
self.acctTypeData = wx.StaticText(self.panel)
self.uploadLimit = wx.StaticText(self.panel, label='Upload Limit:')
self.uploadLimitData = wx.StaticText(self.panel)
self.uploaded = wx.StaticText(self.panel, label='Uploaded in Period:')
self.uploadedData = wx.StaticText(self.panel)
self.cycleExp = wx.StaticText(self.panel, label='Cycle Expiration:')
self.cycleExpData = wx.StaticText(self.panel)
self.acctBox.AddMany([self.userName, self.userNameData,
                     self.acctType, self.acctTypeData,
                     self.uploadLimit, self.uploadLimitData,
                     self.uploaded, self.uploadedData,
                     self.cycleExp, self.cycleExpData])
self.infoBox.Add(self.acctBox, proportion=1,
              flag=wx.EXPAND | wx.ALL, border=10)
self.infoBox.Hide(self.acctBox, recursive=True)

It starts out looking fine:

FGS Start

But when I update the StaticTexts:

self.infoBox.Hide(self.userBox)
self.infoBox.Hide(self.passBox)

self.userNameData = self.conn.publicUserInfo.username
self.acctTypeData = self.PrivledgeLevel(self.conn.user.privilege)
self.uploadLimitData = self.conn.accountLimits.uploadLimit
self.uploadedData = self.conn.syncState.uploaded
self.cycleExpData = self.conn.premiumInfo.premiumExpirationDate

self.infoBox.Show(self.acctBox)
# self.infoBox.Fit(self)
self.infoBox.Layout()
self.connStat.SetLabel('Connected')

Nothing gets updated and the bottom of the FlexGridSizer is cut off:

FGS Empty

And if I add in the self.infoBox.Fit(self), it looks worse:

FGS Fit

What am I missing here to get the StaticTexts to update? I've checked in the debugger and the variables have the values. They're just not being displayed. And how do I get the Dialog to be the correct size to fit everything from the start without calling Fit and resizing? Is hardcoding the Dialog size the only way? Or is there a way to do it dynamically?


Solution

  • You're not actually setting the static texts. You're reassigning their names on your class instance to something else (a string). The only reason your static texts don't go way or get cleaned up by the garbage collector is because they're still referenced by the sizers that contain them.

    In order to actually change their text, you want to set their label. For the userData, for example, you want to call self.userNameData.SetLabel(self.conn.publicUserInfo.username)

    There's extra information about setting labels here in the documentation.