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

2024-05-19 00:03:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我在以下配置中尝试过:

  • 使用Python2.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
  • Ubuntu9.04和Python 2.6.2和wxPython 2.8.9.1

wxDialog不仅仅在Ubuntu上慢。在


Tags: selfaddappinitdefshowwxpythoncell
1条回答
网友
1楼 · 发布于 2024-05-19 00:03:46

我得到了关于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

相关问题 更多 >

    热门问题