wxPython:使用TextCtrl来包装文本会导致包装文本

时间:2016-03-22 17:24:51

标签: python resize wxpython

我一直在尝试使用TextCtrl在应用中包装一长串文字。 TextCtrl对象下面是ListBox对象。两者之间没有边界/填充(即,未设置border)。但是,每当我打开应用程序时,TextCtrl对象和ListBox对象之间总会有一些额外的空白区域。只有当我尝试调整窗口大小(但稍微)时,空间才会消失。下面的代码演示了这个问题。代码下面的两个屏幕截图显示了我的意思。

class MyApp(wx.Frame):
def __init__(self, parent, title):
    super(MyApp, self).__init__(parent, style = wx.DEFAULT_FRAME_STYLE,
                                   title=title, size=(500, 515))
    self.SetBackgroundColour('white')
    self.myGridSizer = wx.GridBagSizer(4,3)

    #App title
    Title = wx.StaticText(self, label = "My Application")
    Title.SetFont(wx.Font(16, family = wx.DEFAULT, style = wx.NORMAL, 
                          weight = wx.BOLD, faceName = 'Consolas'))
    self.myGridSizer.Add(Title, pos = (0, 0), span = (1, 3), flag = wx.EXPAND|wx.ALL, border=10)


    #Input textctrl label
    input_label = wx.StaticText(self, label="Input File: ")
    input_label.SetFont(wx.Font(9, family = wx.DEFAULT, style = wx.NORMAL, 
                          weight = wx.NORMAL, faceName = 'Consolas'))
    self.myGridSizer.Add(input_label, pos = (1, 0), span = (1,1), flag = wx.EXPAND|wx.TOP|wx.RIGHT|wx.LEFT, border = 10)

    #Input textctrl
    input = wx.TextCtrl(self, size=(-1,-1), style = wx.BORDER_SIMPLE)
    self.myGridSizer.Add(input, pos = (1, 1), span = (1,1), flag = wx.EXPAND|wx.TOP|wx.RIGHT, border = 10)

    #Input button
    button = wx.lib.buttons.GenButton(self, id = 1,  
                                      size = (-1, -1), 
                                      label = "OPEN",
                                      style = wx.BORDER_SIMPLE)
    self.myGridSizer.Add(button, pos = (1, 2), span = (1,1), flag = wx.TOP|wx.RIGHT, border = 10)

    #Long text to be wrapped
    note = "This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap."
    noteCtrl = wx.TextCtrl(self, value = note, 
                     style = wx.TE_READONLY|wx.TE_MULTILINE|wx.TE_NO_VSCROLL|wx.BORDER_NONE, size=(-1, -1))
    self.myGridSizer.Add(noteCtrl, pos = (2, 1), span = (1,1), flag = wx.EXPAND|wx.LEFT|wx.RIGHT, border = 10)



    #LISTBOX
    listBoxFields = wx.ListBox(self, choices=[], name='listBox1', style=wx.LB_EXTENDED|wx.BORDER_SIMPLE, size=(-1, -1))
    self.myGridSizer.Add(listBoxFields, pos = (3, 1), span=(1,1), flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border = 10)

    self.myGridSizer.AddGrowableCol(1)
    self.myGridSizer.AddGrowableRow(3)
    self.SetSizer(self.myGridSizer)

if __name__ == "__main__":
    app = wx.App()
    MainFrame = MyApp(None, title = "My Application")
    MainFrame.Show()
    MainFrame.Centre()
    app.MainLoop()

这就是应用程序的样子。请注意绿色箭头指示的空间

在我非常轻微地增加窗口宽度之后,这就是应用程序的样子。请注意红色箭头指示的较窄空间。

在这种情况下,差异并不那么显着。但在我看到的其他一些情况下,额外的空间可能占用窗户的1 / 3-1 / 2。 我想知道如何在不必调整窗口大小的情况下删除这个额外的空间

1 个答案:

答案 0 :(得分:0)

这在Linux下运行良好 我建议你丢失|wx.BORDER_NONE行:

noteCtrl = wx.TextCtrl(self, value = note, 
                 style = wx.TE_READONLY|wx.TE_MULTILINE|wx.TE_NO_VSCROLL|wx.BORDER_NONE, size=(-1, -1))

因为我不认为它是有效的语法 如果没有解决问题,请尝试从|wx.TE_NO_VSCROLL丢失noteCtrl