wxPython wx.TextCtrl dynamically resizing to fill panel width

6.6k views Asked by At

I want a wx.TextCtrl to take the entire remaining width of a panel. It is placed with a wx.StaticText and a wx.Button in a horizontal wx.BoxSizer in a vertical wx.BoxSizer in a wx.lib.scrolledpanel.ScrolledPanel (which is self below):

# create TextCtrl
self.fileNameInput = wx.TextCtrl (self, style=wx.TE_PROCESS_ENTER)
# create horizontal sizer with 3 items
self.fileNameSizer = wx.BoxSizer (wx.HORIZONTAL)
self.fileNameSizer.Add (wx.StaticText (self, -1, 'none'), flag=(wx.ALIGN_CENTER_VERTICAL))
self.fileNameSizer.Add (self.fileNameInput, proportion=1, flag=(wx.EXPAND | wx.ALIGN_CENTER_VERTICAL))
self.fileNameSizer.Add (wx.Button (self, label='Button'), flag=(wx.ALIGN_CENTER_VERTICAL))
# create vertical sizer
self.SetSizer (wx.BoxSizer (wx.VERTICAL))
self.GetSizer ().Add (self.fileNameSizer)

Neither proportion nor wx.EXPAND help to make the TextCtrl larger, presumably because the sizer looks at the TextCtrls own width. But I did not find any style or flag for the `'TextCtrl' to make it variable-width..?

Thanks for ideas!

EDIT: Replaced "..." by something working

1

There are 1 answers

3
Fenikso On BEST ANSWER

I think that what is wrong is this line:

self.GetSizer().Add(self.fileNameSizer)

There should be some of proportion=1 and/or flag=wx.EXPAND to make the nested sizer match its master size.

Something like this:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)

        self.label = wx.StaticText(self.panel, label="Label")
        self.text = wx.TextCtrl(self.panel)
        self.button = wx.Button(self.panel, label="Test")

        self.button1 = wx.Button(self.panel, label="ABOVE")
        self.button2 = wx.Button(self.panel, label="BELLOW")

        self.horizontal = wx.BoxSizer()
        self.horizontal.Add(self.label, flag=wx.CENTER)
        self.horizontal.Add(self.text, proportion=1, flag=wx.CENTER)
        self.horizontal.Add(self.button, flag=wx.CENTER)

        self.vertical = wx.BoxSizer(wx.VERTICAL)
        self.vertical.Add(self.button1, flag=wx.EXPAND)
        self.vertical.Add(self.horizontal, proportion=1, flag=wx.EXPAND)
        self.vertical.Add(self.button2, flag=wx.EXPAND)

        self.panel.SetSizerAndFit(self.vertical)  
        self.Show()


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

BTW: Please stop adding space before ( in method calls. Also I would recommend object oriented approach so you do not loose access to your GUI objects.