3

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

4

1 回答 1

3

我认为有什么问题是这一行:

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

应该有一些proportion=1和/或flag=wx.EXPAND使嵌套大小调整器与其主大小相匹配。

像这样的东西:

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()

顺便说一句:请(在方法调用之前停止添加空格。此外,我会推荐面向对象的方法,这样您就不会失去对 GUI 对象的访问权限。

于 2013-12-06T09:17:19.697 回答