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

标签 python windows wxpython

这似乎是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/

相关文章:

python - wxpython GetStatusText()

python - 如何将变量传递给主机?

python - 如何在 Mac 上从 $PATH 中删除特定路径

python - 使用 NumPy.arange 生成包括右端在内的等距值

c++ - 除了 createfile 和 openfile 之外,还有任何用于获取文件句柄的 Windows API?

windows - Windows Git-bash运行docker.sock

python - 如何用按钮调用另一个类并在类之间传递变量?

python - 如何在 Python - GEKKO 中构建和打印循环生成的优化值列表?

python - 就地赋值和再次使用变量名赋值有什么区别?

c++ - 写入控制台的最有效方法?