wxPython:在 Window-Resize 上动态地将按钮流到下一行

wxPython: Dynamically Flow Buttons to Next Row on Window-Resize

以下 wxPython 示例代码旨在创建一些按钮,然后将它们添加到水平面板,这样当按钮不再适合面板时,它们应该流到新行。

此外,按钮应该改变位置 (increase/decrease 行数)随着用户调整 frame/window 的大小。 (请参阅 http://wxpython-users.1045709.n5.nabble.com/Button-wrap-td2365760.html)。

    import wx

words = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt',
'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua', 'ut', 'enim', 'ad',
'minim', 'veniam', 'quis', 'nostrud', 'exercitation', 'ullamco',
'laboris', 'nisi', 'ut', 'aliquip', 'ex', 'ea', 'commodo',
'consequat', 'duis', 'aute', 'irure', 'dolor', 'in', 'reprehenderit',
'in', 'voluptate', 'velit', 'esse', 'cillum', 'dolore', 'eu',
'fugiat', 'nulla', 'pariatur', 'excepteur', 'sint', 'occaecat',
'cupidatat', 'non', 'proident', 'sunt', 'in', 'culpa', 'qui',
'officia', 'deserunt', 'mollit', 'anim', 'id', 'est', 'laborum']

class Example(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs) 
        self.InitUI()
        self.Ctrls()
        i = 0
        for word in words:
            i += 1
            print " word = ",word, "  ",i
            self.gridSizer.SetCols(5)
            button = wx.Button(self.panel, -1, word, size=wx.Size(70,25))
            self.gridSizer.Add(button, 1, wx.EXPAND)
        self.panel.Layout()

    def InitUI(self):

        self.SetSize((800, 400))
        self.SetTitle('Dynamically Flow Buttons to Next Row on Window-Resize')
        self.Centre()
        self.Show(True)

    def Sizers(self):

        self.gridSizer = wx.GridSizer(cols=0, hgap=1, rows=1, vgap=1)
        self.panel.SetSizer(self.gridSizer)

    def Ctrls(self):

        self.Bind(wx.EVT_SIZE, self.OnFrameSize)
        self.panel = wx.Panel(parent=self,pos=wx.Point(0,0), size=wx.Size(750,550), style=wx.TAB_TRAVERSAL)
        self.Sizers()

    def OnFrameSize(self, event):

        self.UpdateButtonLayout()
        event.Skip()

    def UpdateButtonLayout(self):

        panelwidth = self.panel.GetSize().width
        print panelwidth
        factor = panelwidth / 70
        print "Factor: ", factor
        self.gridSizer.SetCols(factor)
        self.panel.Layout()

def main():

    ex = wx.App()
    Example(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()

当我 运行 这个例子时,我得到以下错误;

Traceback (most recent call last):
  File "test.py", line 68, in <module>
    main()
  File "test.py", line 64, in main
    Example(None)
  File "test.py", line 26, in __init__
    self.gridSizer.Add(button, 1, wx.EXPAND)
  File "/usr/local/lib/wxPython-3.0.2.0/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core.py", line 14457, in Add
    return _core_.Sizer_Add(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "Assert failure" failed at /BUILD/wxPython-src-3.0.2.0/src/common/sizer.cpp(1401) in DoInsert(): too many items (6 > 5*1) in grid sizer (maybe you should omit the number of either rows or columns?)

问题出在第 26 行。当我注释掉它时,我只看到一个按钮。有人可以建议一些更改以使其工作吗?谢谢

您要查找的是wx.WrapSizer。我稍微更新了您的代码以改为使用它:

import wx

words = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt',
'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua', 'ut', 'enim', 'ad',
'minim', 'veniam', 'quis', 'nostrud', 'exercitation', 'ullamco',
'laboris', 'nisi', 'ut', 'aliquip', 'ex', 'ea', 'commodo',
'consequat', 'duis', 'aute', 'irure', 'dolor', 'in', 'reprehenderit',
'in', 'voluptate', 'velit', 'esse', 'cillum', 'dolore', 'eu',
'fugiat', 'nulla', 'pariatur', 'excepteur', 'sint', 'occaecat',
'cupidatat', 'non', 'proident', 'sunt', 'in', 'culpa', 'qui',
'officia', 'deserunt', 'mollit', 'anim', 'id', 'est', 'laborum']

class Example(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs) 
        self.InitUI()
        self.Ctrls()
        i = 0
        for word in words:
            i += 1
            print " word = ",word, "  ",i
            button = wx.Button(self.panel, -1, word, size=wx.Size(70,25))
            self.wrapSizer.Add(button, 1, wx.EXPAND)
        self.Show(True)
        self.panel.Layout()

    def InitUI(self):
        self.SetSize((800, 400))
        self.SetTitle('Dynamically Flow Buttons to Next Row on Window-Resize')
        self.Centre()


    def Sizers(self):
        self.wrapSizer = wx.WrapSizer()
        self.panel.SetSizer(self.wrapSizer)

    def Ctrls(self):
        self.panel = wx.Panel(parent=self,pos=wx.Point(0,0), size=wx.Size(750,550), style=wx.TAB_TRAVERSAL)
        self.Sizers()



def main():

    ex = wx.App()
    Example(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()

请注意,您不能设置列数。您可以在以下位置阅读有关此有趣小部件的更多信息:

迟到的答案,但使用 FlexGridSizer 将获得相同的结果,其中行数设置为零。
在问题代码中,您还必须删除行 self.gridSizer.SetCols(5)

import wx

words = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt',
'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua', 'ut', 'enim', 'ad',
'minim', 'veniam', 'quis', 'nostrud', 'exercitation', 'ullamco',
'laboris', 'nisi', 'ut', 'aliquip', 'ex', 'ea', 'commodo',
'consequat', 'duis', 'aute', 'irure', 'dolor', 'in', 'reprehenderit',
'in', 'voluptate', 'velit', 'esse', 'cillum', 'dolore', 'eu',
'fugiat', 'nulla', 'pariatur', 'excepteur', 'sint', 'occaecat',
'cupidatat', 'non', 'proident', 'sunt', 'in', 'culpa', 'qui',
'officia', 'deserunt', 'mollit', 'anim', 'id', 'est', 'laborum']

class Example(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs) 
        self.InitUI()
        self.Ctrls()
        i = 0
        for word in words:
            i += 1
            print " word = ",word, "  ",i
            button = wx.Button(self.panel, -1, word, size=wx.Size(70,25))
            self.gridSizer.Add(button, 1, wx.EXPAND)
        self.panel.Layout()

    def InitUI(self):

        self.SetSize((800, 400))
        self.SetTitle('Dynamically Flow Buttons to Next Row on Window-Resize')
        self.Centre()
        self.Show(True)

    def Sizers(self):

        self.gridSizer = wx.FlexGridSizer(0,5,1,1)
        self.panel.SetSizer(self.gridSizer)

    def Ctrls(self):

        self.Bind(wx.EVT_SIZE, self.OnFrameSize)
        self.panel = wx.Panel(parent=self,pos=wx.Point(0,0), size=wx.Size(750,550), style=wx.TAB_TRAVERSAL)
        self.Sizers()

    def OnFrameSize(self, event):

        self.UpdateButtonLayout()
        event.Skip()

    def UpdateButtonLayout(self):

        panelwidth = self.panel.GetSize().width
        print panelwidth
        factor = panelwidth / 70
        print "Factor: ", factor
        self.gridSizer.SetCols(factor)
        self.panel.Layout()

def main():

    ex = wx.App()
    Example(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()