wxpython:GridBagSizer使我感到困惑

时间:2019-03-08 15:04:48

标签: python wxpython

我是python GUI和wxpython的新手。现在我正面临以下问题。

此处的代码为

import wx

class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title)

        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):
        panel = wx.Panel(self)
        sizer = wx.GridBagSizer(0, 0)

        text = wx.StaticText(panel, label="Name:")
        sizer.Add(text, pos=(0, 0), flag=wx.ALL, border=5)

        tc = wx.TextCtrl(panel)
        sizer.Add(tc, pos=(0, 1), span=(1, 2), flag=wx.EXPAND | wx.ALL, border=5)

        text1 = wx.StaticText(panel, label="address")
        sizer.Add(text1, pos=(1, 0), flag=wx.ALL, border=5)

        tc1 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        sizer.Add(tc1, pos=(1, 1), span=(1, 3), flag=wx.EXPAND | wx.ALL, border=5)

        text11 = wx.StaticText(panel, label="address2")
        sizer.Add(text11, pos=(1, 4), flag=wx.ALL, border=5)

        tc11 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        sizer.Add(tc11, pos=(1, 5), span=(1, 3), flag=wx.EXPAND | wx.ALL, border=5)

        text2 = wx.StaticText(panel, label="age")
        sizer.Add(text2, pos=(2, 0), flag=wx.ALL, border=5)

        tc2 = wx.TextCtrl(panel)
        sizer.Add(tc2, pos=(2, 1), flag=wx.ALL, border=5)

        text3 = wx.StaticText(panel, label="Mob.No")
        sizer.Add(text3, pos=(2, 2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)

        tc3 = wx.TextCtrl(panel)
        sizer.Add(tc3, pos=(2, 3), flag=wx.EXPAND | wx.ALL, border=5)

        text4 = wx.StaticText(panel, label="Description")
        sizer.Add(text4, pos=(3, 0), flag=wx.ALL, border=5)

        tc4 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        sizer.Add(tc4, pos=(3, 1), span=(1, 3), flag=wx.EXPAND | wx.ALL, border=5)
        sizer.AddGrowableRow(3)

        buttonOk = wx.Button(panel, label="Ok")
        buttonClose = wx.Button(panel, label="Close")

        sizer.Add(buttonOk, pos=(4, 2), flag=wx.ALL, border=5)
        sizer.Add(buttonClose, pos=(4, 3), flag=wx.ALL, border=5)

        panel.SetSizerAndFit(sizer)


app = wx.App()
Example(None, title='GridBag Demo')
app.MainLoop()

结果是

enter image description here

让我感到困惑的是标签address2。它显然没有跨越三列。

text11 = wx.StaticText(panel, label="address2")
sizer.Add(text11, pos=(1, 4), flag=wx.ALL, border=5)

tc11 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
sizer.Add(tc11, pos=(1, 5), span=(1, 3), flag=wx.EXPAND | wx.ALL, border=5)

但是address2标签的代码与address标签相同,除了参数pos。有人可以解释一下。

env:python3.7 / wxpython4.0.4(最新)

1 个答案:

答案 0 :(得分:1)

这3列的定义是什么?
您是否为sizer的第5,6和7列定义了任何内容?
如果没有与sizer一起使用的东西,就不知道这些列的大小,因此您必须给它一些东西来使用,或者定义一个空的单元格大小。

import wx

class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(750,-1))

        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):
        panel = wx.Panel(self, -1)
        sizer = wx.GridBagSizer(0, 7)

        text = wx.StaticText(panel, label="Name:")
        sizer.Add(text, pos=(0, 0), flag=wx.ALL, border=5)
#
# place some dummy text to give the sizer something to work with
#
#        dummy4 = wx.StaticText(panel, label="something")
#        sizer.Add(dummy4, pos=(0, 4), flag=wx.ALL, border=5)
#        dummy5 = wx.StaticText(panel, label="something")
#        sizer.Add(dummy5, pos=(0, 5), flag=wx.ALL, border=5)
#        dummy6 = wx.StaticText(panel, label="something")
#        sizer.Add(dummy6, pos=(0, 6), flag=wx.ALL, border=5)
#        dummy7 = wx.StaticText(panel, label="something")
#        sizer.Add(dummy7, pos=(0, 7), flag=wx.ALL, border=5)
#
# or set a cell size for empty cells
#
        sizer.SetEmptyCellSize((130,20))

        tc = wx.TextCtrl(panel)
        sizer.Add(tc, pos=(0, 1), span=(1, 2), flag=wx.EXPAND | wx.ALL, border=5)

        text1 = wx.StaticText(panel, label="address")
        sizer.Add(text1, pos=(1, 0), flag=wx.ALL, border=5)

        tc1 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        sizer.Add(tc1, pos=(1, 1), span=(1, 3), flag=wx.EXPAND | wx.ALL, border=5)

        text11 = wx.StaticText(panel, label="address2")
        sizer.Add(text11, pos=(1, 4), flag=wx.ALL, border=5)

        tc11 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        sizer.Add(tc11, pos=(1, 5), span=(1, 3), flag=wx.EXPAND | wx.ALL, border=5)

        text2 = wx.StaticText(panel, label="age")
        sizer.Add(text2, pos=(2, 0), flag=wx.ALL, border=5)

        tc2 = wx.TextCtrl(panel)
        sizer.Add(tc2, pos=(2, 1), flag=wx.ALL, border=5)

        text3 = wx.StaticText(panel, label="Mob.No")
        sizer.Add(text3, pos=(2, 2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)

        tc3 = wx.TextCtrl(panel)
        sizer.Add(tc3, pos=(2, 3), flag=wx.EXPAND | wx.ALL, border=5)

        text4 = wx.StaticText(panel, label="Description")
        sizer.Add(text4, pos=(3, 0), flag=wx.ALL, border=5)

        tc4 = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        sizer.Add(tc4, pos=(3, 1), span=(1, 3), flag=wx.EXPAND | wx.ALL, border=5)
        sizer.AddGrowableRow(3)

        buttonOk = wx.Button(panel, label="Ok")
        buttonClose = wx.Button(panel, label="Close")

        sizer.Add(buttonOk, pos=(4, 2), flag=wx.ALL, border=5)
        sizer.Add(buttonClose, pos=(4, 3), flag=wx.ALL, border=5)

        panel.SetSizerAndFit(sizer)

app = wx.App()
Example(None, title='GridBag Demo')
app.MainLoop()

p.s。如果将address2放在地址1下方,则布局可能会更好。

enter image description here