放課後
放課後

Reputation: 736

wxpython: The GridBagSizer made me confused

I am new with python GUI and wxpython also. Now im facing following problem.

Here the code is

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

Here the result is

enter image description here

What make me confused is the label address2.It does not span three columns obviously.

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)

But the code of address2 label is same with address label except param pos.Can someone explain this.

env: python3.7/wxpython4.0.4(newest)

Upvotes: 0

Views: 262

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

What is the definition of those 3 columns?
Have you defined anything for columns 5,6 and 7 for the sizer to work with?
Without something to work with the sizer has no idea how big those columns are, so you must either give it something to work with or define an empty cell size.

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. The layout would probably work better if you simply placed address2 underneath address 1

enter image description here

Upvotes: 1

Related Questions