wxPython wx.TextCtrl动态调整大小以填充面板宽度

时间:2013-12-05 18:03:36

标签: wxpython width textctrl

我希望wx.TextCtrl获取面板的整个剩余宽度。它在wx.StaticText的垂直wx.Button中的水平wx.BoxSizer中放置wx.BoxSizerwx.lib.scrolledpanel.ScrolledPanel(下面为self) :

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

proportionwx.EXPAND都无法使TextCtrl更大,大概是因为sizer会查看TextCtrl自己的宽度。但是我没有找到“TextCtrl”的任何样式或标志来使它变宽...?

感谢您的想法!

编辑: 用某种工作取代“......”

1 个答案:

答案 0 :(得分:2)

我认为错误的是这一行:

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

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

这样的事情:

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对象的访问权限。