WrapSize 和 Scrollbar :在启动时未绘制的小部件以后不会绘制 [英] WrapSize and Scrollbar : widgets not drawn at startup won't draw later
问题描述
使用来自仅带有垂直滚动条和 WrapSizer 的 ScrolledPanel 的公认解决方案,这是一种使用垂直滚动条将一些自定义 MyControl
放入 WrapSizer
的方法:
By using the accepted solution from ScrolledPanel with vertical scrollbar only and WrapSizer, here is a way of putting some custom MyControl
into WrapSizer
with a vertical scrollbar :
不幸的是,即使在移动滚动条之后,启动时未绘制的项目也不会在以后绘制.这里只绘制了前 9 个按钮(在 20 个中):
Unfortunately, the items that are not drawn at startup won't draw later, even after moving the scrollbar. Here only the first 9 buttons (among 20) are drawn :
import wx
import wx.lib.scrolledpanel as scrolled
class MyControl(wx.PyControl):
def __init__(self, parent, i):
wx.PyControl.__init__(self, parent, wx.ID_ANY, wx.DefaultPosition, size=(100,100))
wx.Button(self, wx.ID_ANY, "btn %d" % i, (0,0), (50,50), 0)
# some other things here
class MyPanel(scrolled.ScrolledPanel):
def __init__(self, parent):
scrolled.ScrolledPanel.__init__(self, parent, style=wx.VSCROLL)
self.sizer = wx.WrapSizer()
self.SetupScrolling(scroll_x = False)
for i in range(1, 20):
btn = MyControl(self, i)
self.sizer.Add(btn, 0, wx.ALL, 10)
self.SetSizer(self.sizer)
self.Bind(wx.EVT_SIZE, self.onSize)
def onSize(self, evt):
size = self.GetSize()
vsize = self.GetVirtualSize()
self.SetVirtualSize((size[0], vsize[1]))
app = wx.App(redirect=False)
frame = wx.Frame(None, size=(400,400))
MyPanel(frame)
frame.Show()
app.MainLoop()
如何在这样的WrapSizer
中正确绘制所有项目?
How to correctly draw all the items in such a WrapSizer
?
推荐答案
我尝试在 MyControl 类中使用 sizer 并且显示所有按钮:
I tried using a sizer in the MyControl class and all the buttons were displayed:
class MyControl(wx.PyControl):
def __init__(self, parent, i):
wx.PyControl.__init__(self, parent, wx.ID_ANY, wx.DefaultPosition, size=(100,100))
# Just saving the button in a variable
btn = wx.Button(self, wx.ID_ANY, "btn %d" % i, (0,0), (50,50), 0)
# Creating sizer and placing the button in sizer
sizer = wx.BoxSizer()
sizer.Add(btn)
self.SetSizer(sizer)
您可以随意使用 sizer 参数,让它们以您想要的方式填充空间.这是您正在寻找的解决方案类型吗?
You can mess around with the sizer arguments to make them fill up the space how you want it. Is this the type of solution you were looking for?
这篇关于WrapSize 和 Scrollbar :在启动时未绘制的小部件以后不会绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!