簡體   English   中英

wxPython-替換wxFrame的面板時重繪錯誤

[英]wxPython - Redrawing Error when replacing wxFrame's Panel

我是第一次創建一個小的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

1)我相信Sizer將允許您將元素插入到它們的現有順序中。 那可能會快一點。

2)我看不到您在OSX上描述的行為,但是猜測,嘗試在layoutElements中在self.Show()之前調用self.Layout()嗎?

我有一個類似的問題,面板會被壓到右上角。 我通過調用panel.Fit()解決了這個問題。

在您的示例中,您應該在self.panel.SetSizer(vbox)之后調用self.panel.Fit() self.panel.SetSizer(vbox)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM