gpt4 book ai didi

python - 为什么 wxGridSizer 在 wxDialog 上的初始化比在 wxFrame 上的初始化慢得多?

转载 作者:可可西里 更新时间:2023-11-01 10:30:46 33 4
gpt4 key购买 nike

这似乎是windows特有的,这里有一个重现效果的例子:

import wx


def makegrid(window):
grid = wx.GridSizer(24, 10, 1, 1)
window.SetSizer(grid)
for i in xrange(240):
cell = wx.Panel(window)
cell.SetBackgroundColour(wx.Color(i, i, i))
grid.Add(cell, flag=wx.EXPAND)


class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
makegrid(self)


class TestDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
makegrid(self)


class Test(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
btn1 = wx.Button(self, label="Show Frame")
btn2 = wx.Button(self, label="Show Dialog")
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
sizer.Add(btn1, flag=wx.EXPAND)
sizer.Add(btn2, flag=wx.EXPAND)
btn1.Bind(wx.EVT_BUTTON, self.OnShowFrame)
btn2.Bind(wx.EVT_BUTTON, self.OnShowDialog)

def OnShowFrame(self, event):
TestFrame(self).Show()

def OnShowDialog(self, event):
TestDialog(self).ShowModal()


app = wx.PySimpleApp()
app.TopWindow = Test()
app.TopWindow.Show()
app.MainLoop()

我已经在以下配置上尝试过:

  • 带有 Python 2.5.4 和 wxPython 2.8.10.1 的 Windows 7
  • 带有 Python 2.5.2 和 wxPython 2.8.7.1 的 Windows XP
  • 带有 Python 2.6.0 和 wxPython 2.8.9.1 的 Windows XP
  • Ubuntu 9.04 与 Python 2.6.2 和 wxPython 2.8.9.1

wxDialog 不仅在 Ubuntu 上很慢。

最佳答案

我在 wxPython-users mailing list 上收到了回复,可以通过在显示对话框之前显式调用 Layout 来解决此问题。

This is really weird...

My guess is that this is due to Windows and wxWidgets not dealing very well with overlapping siblings, and so when the sizer is doing the initial layout and moving all the panels from (0,0) to where they need to be that something about the dialog is causing all of them to be refreshed and repainted at each move. If you instead do the initial layout before the dialog is shown then it is just as fast as the frame.

You can do this by adding a call to window.Layout() at the end of makegrid.

-- Robin Dunn

关于python - 为什么 wxGridSizer 在 wxDialog 上的初始化比在 wxFrame 上的初始化慢得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1198067/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com