简体   繁体   中英

wxpython use the same static text twice in FlexGridSizer.AddMany

I would like to duplicate column headings in my first python GUI. I have tried the following

    bfont = wx.Font(10,wx.DEFAULT,wx.NORMAL,wx.BOLD)
    angle = wx.StaticText(panel,label="Angle")
    angle.SetFont(bfont)
    count_c = wx.StaticText(panel,label="Counts (C)")
    count_c.SetFont(bfont)
    count_u = wx.StaticText(panel,label="Counts (U)")
    count_u.SetFont(bfont)

    fgs.AddMany([(angle),(count_c), (count_u),
                 (angle),(count_c), (count_u)])


    vbox.Add(fgs, proportion=1,flag=wx.ALL|wx.EXPAND,border=5)

However this only shows me the second set of headers. How can this be done?

You cannot add the same widget to two different locations. Instead, you'll have to create separate widgets for each row. Since you want the same thing on each row, you can use a loop:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        bfont = wx.Font(10,wx.DEFAULT,wx.NORMAL,wx.BOLD)

        vbox = wx.BoxSizer(wx.VERTICAL)
        fgs = wx.FlexGridSizer(rows=2, cols=3, vgap=5, hgap=5)

        # add two rows of widgets to the sizer
        widgets = []
        for i in range(2):
            angle = wx.StaticText(self,label="Angle")
            angle.SetFont(bfont)
            widgets.append(angle)

            count_c = wx.StaticText(self,label="Counts (C)")
            count_c.SetFont(bfont)
            widgets.append(count_c)

            count_u = wx.StaticText(self,label="Counts (U)")
            count_u.SetFont(bfont)
            widgets.append(count_u)

        fgs.AddMany(widgets)
        vbox.Add(fgs, proportion=1,flag=wx.ALL|wx.EXPAND,border=5)
        self.SetSizer(vbox)

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM