0

我第一次创建了一个小型 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
4

2 回答 2

1

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

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

于 2009-02-18T17:25:31.290 回答
0

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

在您的示例中,您应该调用self.panel.Fit()afterself.panel.SetSizer(vbox)

于 2009-05-06T08:29:12.843 回答