WrapSize and Scrollbar : widgets not drawn at startup won't draw later

独自空忆成欢 提交于 2019-12-23 03:53:13

问题


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 :

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

How to correctly draw all the items in such a WrapSizer ?



回答1:


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)

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?




回答2:


It's very suspicious to use both sizers and explicit wxEVT_SIZE handler. Basically, your sizer is not used at all. Remove your size event handler and check what happens.




回答3:


Solution:

Add another panel into the Sizer of "MyControl".

You can put the button and text into this panel as you like.

class MyControl(wx.PyControl):
    def __init__(self, parent, i):
        wx.PyControl.__init__(self, parent, wx.ID_ANY, wx.DefaultPosition, size=(100,100))

        panel = wx.Panel(self)
        btn  =  wx.Button(panel, wx.ID_ANY, "btn %d" % i, (0,0), (50,50), 0)
        txt1 = wx.StaticText(panel, -1, 'blah', (50,50))
        txt2 = wx.StaticText(panel, -1, 'blah2', (50,80))
        sizer = wx.BoxSizer()
        sizer.Add(panel)
        self.SetSizer(sizer)


来源:https://stackoverflow.com/questions/21438844/wrapsize-and-scrollbar-widgets-not-drawn-at-startup-wont-draw-later

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!