2011-12-30 64 views
0

创建wx.Panel时,根据主窗口是否已调用Show(),我遇到了非常不同的行为。在现有窗口中的wxPython面板:缓慢和小

用下面的代码,应用程序快速打开,并创建了之前创建的。使用“新建”重新创建它时会有多秒的滞后,而重新制作200个文本。此外,MainPanel不会展开占据整个窗口的大小,直到窗口被调整大小。

这确实是面板创建而不是面板销毁的问题;如果我在MainFrame的init中删除MainPanel创建,那么New操作的速度与&的速度相同。

两个问题:

有什么我可以做MainFrame.Show()后加快面板创造?

MainFrame.Show()之后创建MainPanel以使MainPanel扩展到其父级的大小后需要做些什么?

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

import wx 
import wx.lib.scrolledpanel 

class MainPanel(wx.lib.scrolledpanel.ScrolledPanel): 
    def __init__(self,parent): 
     wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent) 
     self.SetupScrolling() 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     for i in range(1,200): 
      sizer.Add(wx.StaticText(self, wx.ID_ANY, "I'm static text")) 
     self.SetSizer(sizer) 
     self.SetAutoLayout(True) 

class MainFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, title="FrameTest", size=(600,800)) 
     self.InitMenu() 
     self.panel = None 
     self.panel = MainPanel(self) 

    def InitMenu(self): 
     self.menuBar = wx.MenuBar() 
     menuFile = wx.Menu() 
     menuFile.Append(wx.ID_NEW, "&New") 
     self.Bind(wx.EVT_MENU, self.OnNew, id=wx.ID_NEW) 
     self.menuBar.Append(menuFile, "&File") 
     self.SetMenuBar(self.menuBar) 

    def OnNew(self, evt): 
     if self.panel: 
      self.panel.Destroy() 
     self.panel = MainPanel(self) 

if __name__ == "__main__": 
    app = wx.App(0) 
    frame = MainFrame() 
    frame.Show() 
    app.MainLoop() 

UPDATE:华金的SendSizeEvent()肯定解决了第一个问题。第二,我发现隐藏容器工作得很好。我猜测,在窗口显示之后,它试图在每个新窗口小部件之后不必要地重新(显示/布局/某些东西),这会减慢它的速度。如果我将隐藏&显示添加到面板的初始化,那么没有更多的滞后,它在两种情况下都能正常工作。

class MainPanel(wx.lib.scrolledpanel.ScrolledPanel): 
    def __init__(self,parent): 
     wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent) 
     self.SetupScrolling() 
     self.Hide() 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     for i in range(1,200): 
      sizer.Add(wx.StaticText(self, wx.ID_ANY, "I'm static text")) 
     self.SetSizer(sizer) 
     self.SetAutoLayout(True) 
     self.Show() 

回答

1

如果Frame感觉SizeEvent,面板将得到正确的大小。所以这适用于你的第二个问题:

def OnNew(self, evt): 
    if self.panel: 
     self.panel.Destroy() 
    self.panel = MainPanel(self) 
    self.SendSizeEvent() 

使用sizer控制窗口和小部件变得更容易。通过在主框架中使用sizer,您可以使用sizer Layout方法来适应小部件。

无法找到加快面板重写的方法。但是你可以通过不删除的面板,但结算筛分器,而不是削弱离奇的视觉效果(你不需要SendSizeEvent()这种情况):

class MainPanel(wx.lib.scrolledpanel.ScrolledPanel): 
    def __init__(self,parent): 
     wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent) 
     self.SetupScrolling() 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.fill() 
     self.SetSizer(self.sizer) 

    def fill(self): 
     tup = [wx.StaticText(self, wx.ID_ANY, "I'm static text") for i in range(200)] 
     self.sizer.AddMany(tup) 
     self.Layout() 

class MainFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, title="FrameTest", size=(600,800)) 
     self.InitMenu() 
     self.panel = None 
     self.panel = MainPanel(self) 

    def InitMenu(self): 
     self.menuBar = wx.MenuBar() 
     menuFile = wx.Menu() 
     menuFile.Append(wx.ID_NEW, "&New") 
     self.Bind(wx.EVT_MENU, self.OnNew, id=wx.ID_NEW) 
     self.menuBar.Append(menuFile, "&File") 
     self.SetMenuBar(self.menuBar) 

    def OnNew(self, evt): 
     if self.panel: 
      self.panel.sizer.Clear() 
     self.panel.fill() 
+0

SendSizeEvent()肯定解决了第一个问题。 – 2011-12-31 21:46:59

+0

清除sizer而不是删除是有趣的,虽然它不能帮助我的真实情况。我有一个空的窗口,当用户打开一个文件时创建一个面板,所以没有以前的面板“不删除”。尽管我发现了Hide()ing和Show()帮助,但我更新了这个问题。谢谢! – 2011-12-31 21:54:57