我正在为我的应用程序构建一个面板,其中包含大量缩略图;它们不会全部放在窗口中,所以我使用 ScrolledWindow 来保存它们并允许用户滚动查看它们。
生成缩略图图像需要一些时间,因此我尝试优先处理当前在窗口中可见的缩略图图像的生成。我还想捕捉哪些缩略图是可见的,这样如果我添加或删除缩略图的子集,我就知道滚动条的位置,以尽可能多地包含以前可见的缩略图。
我遇到的问题是 EVT_SCROLLWIN 似乎是在子窗口重新定位到 ScrolledWindow 的客户区域之前发布的;换句话说,我正在测量滚动条移动之前的可见对象。有没有办法:a)强制 ScrolledWindow 更新子窗口位置,b)可靠地确定新的客户端窗口偏移量与现在相比,或者 c)在此更新发生后调用事件处理程序?
如果重要的话,我正在这台计算机上运行 Python 2.7.11 和 wxPython 3.0.2.0。
一段代码如下:
class ThumbsViewer(wx.ScrolledWindow):
def __init__(self, parent, info, style = wx.BORDER_RAISED | wx.HSCROLL):
wx.ScrolledWindow.__init__(self, parent = parent, id = wx.ID_ANY, size = wx.DefaultSize,
pos = wx.DefaultPosition, style = style, name = "ThumbsViewer")
self.__info__ = info
self.__bitmapctrls__ = []
self.__selected__ = []
self.__lastclicked__ = []
self.Bind(wx.EVT_SCROLLWIN, self.__OnScroll__)
# ... Code to generate placeholder thumbnails, arrange buttons, size window, etc. ...
# ... Various functions to handle clicking on thumbnails, etc. ...
# EVT_SCROLLWIN Handler - Measure client window positions and determine which are
# visible on the screen.
def __OnScroll__(self, event):
if event:
event.Skip()
priority = []
clientsize = self.GetClientSize()
for ctrl in self.__bitmapctrls__:
pos = ctrl.GetPosition()
size = ctrl.GetSize()
# Test to see if any part of the thumbnail control falls within the client
# area...append to the priority list if it does.
# This appears to be where the problem is - the child window positions
# have not been updated to reflect the new scrollbar position yet.
if (((pos[0] >= 0 and pos[0] <= clientsize[0]) or (pos[0] + size[0] >= 0 and pos[0] + size[0] <= clientsize[0]))
and ((pos[1] >= 0 and pos[1] <= clientsize[1]) or (pos[1] + size[1] >= 0 and pos[1] + size[1] <= clientsize[1]))):
priority.append(ctrl.GetPage())
self.__info__.SetPriorityPages(priority)