【问题标题】:wxPython - Redrawing Error when replacing wxFrame's PanelwxPython - 替换 wxFrame 的面板时重绘错误
【发布时间】:2010-10-08 09:36:02
【问题描述】:

我是第一次创建一个小型 wxPython 实用程序,但遇到了一个问题。

我想将组件添加到已创建的框架中。为此,我将销毁框架的旧面板,并创建一个包含所有新组件的新面板。

1:有没有更好的方法可以动态地向面板添加内容?

2:为什么在下面的示例中,我会收到一个奇怪的重绘错误,其中面板仅在左上角绘制,并且在调整大小时,面板正确绘制? (WinXP、Python 2.5、最新的 wxPython)

感谢您的帮助!

    import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'TimeTablr')


        #Variables
        self.iCalFiles = ['Empty', 'Empty', 'Empty']
        self.panel = wx.Panel(self, -1)
        self.layoutElements()        


    def layoutElements(self):
        self.panel.Destroy()
        self.panel = wx.Panel(self, -1)
        #Buttons
        self.getFilesButton = wx.Button(self.panel, 1, 'Get Files')
        self.calculateButton = wx.Button(self.panel, 2, 'Calculate')
        self.quitButton = wx.Button(self.panel, 3, 'Quit Application')

        #Binds
        self.Bind(wx.EVT_BUTTON, self.Quit, id=3)
        self.Bind(wx.EVT_BUTTON, self.getFiles, id=1)

        #Layout Managers
        vbox = wx.BoxSizer(wx.VERTICAL)

        #Panel Contents
        self.ctrlsToDescribe = []
        self.fileNames = []
        for iCalFile in self.iCalFiles:
            self.ctrlsToDescribe.append(wx.TextCtrl(self.panel, -1))
            self.fileNames.append(wx.StaticText(self.panel, -1, iCalFile))

        #Add Components to Layout Managers
        for i in range(0, len(self.ctrlsToDescribe)):
            hboxtemp = wx.BoxSizer(wx.HORIZONTAL)
            hboxtemp.AddStretchSpacer()
            hboxtemp.Add(self.fileNames[i], 1, wx.EXPAND)
            hboxtemp.AddStretchSpacer()
            hboxtemp.Add(self.ctrlsToDescribe[i], 2, wx.EXPAND)
            hboxtemp.AddStretchSpacer()
            vbox.Add(hboxtemp)

        finalHBox = wx.BoxSizer(wx.HORIZONTAL)
        finalHBox.Add(self.getFilesButton)
        finalHBox.Add(self.calculateButton)
        finalHBox.Add(self.quitButton)

        vbox.Add(finalHBox)
        self.panel.SetSizer(vbox)
        self.Show()


    def Quit(self, event):
        self.Destroy()

    def getFiles(self, event):
        self.iCalFiles = ['Example1','Example1','Example1','Example1','Example1','Example1']
        self.layoutElements()
        self.Update()



app = wx.App()
MainFrame()
app.MainLoop()
del app

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    1) 我相信 Sizer 会让您将元素插入到它们的现有顺序中。那可能会快一点。

    2) 我没有看到您在 OSX 上描述的行为,但猜测一下,尝试在 layoutElements 中的 self.Show() 之前调用 self.Layout()?

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,面板会被挤压到右上角。我通过调用panel.Fit()解决了它。

      在你的例子中,你应该在self.panel.SetSizer(vbox)之后调用self.panel.Fit()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-06
        • 2017-03-31
        • 2012-06-01
        • 2014-11-03
        • 1970-01-01
        • 1970-01-01
        • 2010-11-10
        • 1970-01-01
        相关资源
        最近更新 更多