以下wxPython示例代码用于创建一些按钮,然后将它们添加到水平面板中,使得按钮在不再适合面板时应该流向新行。
此外,按钮应改变位置 (当用户调整框架/窗口大小时)(增加/减少行数)。 (请参阅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行。当我发表评论时,我只看到一个按钮。有人可以提出一些改变以使其有效吗?感谢。
答案 0 :(得分:1)
您要找的是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()
请注意,您无法设置列数。您可以通过以下方式阅读有关此有趣小部件的更多信息:
答案 1 :(得分:0)
迟到的答案但是使用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()