无法清除wxBoxSizer的内容

时间:2013-06-24 09:14:52

标签: python wxpython wxwidgets

由于某种原因,我的sizer.Clear()似乎无法正常工作。

据我所知,从文档中可以看出它应该有用。

我做错了会导致这种行为吗?

import wx
import os
import sys
import time
import string
import urllib2

class MainWindow(wx.Frame):
  def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
               size=wx.DefaultSize, style=wx.MINIMIZE_BOX | wx.CLOSE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION):

    wx.Frame.__init__(self, parent, id, title, pos, size, style)
    panel = wx.Panel(self)
    sizer = wx.BoxSizer(wx.VERTICAL)
    panel.SetSizer(sizer)

    self.sizer = sizer
    self.panel = panel

    self.ShowLoginBox()



  def ShowLoginBox(self):
    sizer = wx.FlexGridSizer(rows=4, cols=2, hgap=15, vgap=10)
    sizer.AddGrowableCol(1, 1)

    login_url = wx.TextCtrl(self.panel, 1, size=(150, -1))
    label_url = wx.StaticText(self.panel, -1, "URL:")
    sizer.Add(label_url, 0, wx.LEFT | wx.TOP| wx.RIGHT, 30)
    sizer.Add(login_url, 1, wx.EXPAND | wx.TOP | wx.RIGHT, 30)

    login_username = wx.TextCtrl(self.panel, 1, size=(150, -1))
    label_username = wx.StaticText(self.panel, -1, "Username:")
    sizer.Add(label_username, 0, wx.LEFT | wx.RIGHT, 30)
    sizer.Add(login_username, 1, wx.EXPAND | wx.RIGHT, 30)

    login_password = wx.TextCtrl(self.panel, 1, size=(150, -1), style=wx.TE_PASSWORD)
    label_password = wx.StaticText(self.panel, -1, "Password:")
    sizer.Add(label_password, 0, wx.LEFT | wx.RIGHT, 30)
    sizer.Add(login_password, 1, wx.EXPAND | wx.RIGHT, 30)

    btn_process = wx.Button(self.panel, -1, "&Login")
    self.panel.Bind(wx.EVT_BUTTON, self.OnSubmit, btn_process)
    sizer.Add(btn_process, 0, wx.LEFT, 30)

    login_url.SetValue("http://example.com")
    login_username.SetValue("admin")
    login_password.SetValue("pass")

    self.login_url = login_url
    self.login_username = login_username
    self.login_password = login_password

    self.sizer.Clear()
    self.sizer.Add(sizer, 1, wx.EXPAND)
    self.SetSizeWH(330, 250)
    self.Center()



  def OnSubmit(self, event):
    user_url  = self.login_url.GetValue()
    user_name = self.login_username.GetValue()
    user_pass = self.login_password.GetValue()

    # login info is used to get a URL
    print user_url, user_name, user_pass

    # if 200 OK
    self.ShowNew()



  def ShowNew(self):
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    content = wx.ListCtrl(self.panel, -1, size=(780, 400), style=wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_HRULES | wx.LC_VRULES)
    content.InsertColumn(0, 'URL', width=745)
    sizer.Add(content, 1, wx.EXPAND)

    # populate with data from URL

    self.SetSizeWH(800, 520)
    self.Center()
    self.sizer.Clear()
    self.sizer.Add(sizer)



if __name__ == '__main__':
  home_path = os.path.dirname(os.path.realpath(__file__))

  app = wx.PySimpleApp()
  frame = MainWindow(None, title="Test", size=(800, 520))
  frame.SetBackgroundColour("#ffffff")
  frame.Show()
  app.MainLoop()

1 个答案:

答案 0 :(得分:4)

使用 sizer.Clear(True)代替 sizer.Clear(),它应该有效。

解释

如果发送到Clear()方法的参数为True,则“子窗口也将被删除”(http://docs.wxwidgets.org/2.8/wx_wxsizer.html#wxsizerclear)。

你想要清除wxSizer的子代: wxStaticText,wxTextCtrl,wxButton ,它们都是从 wxControl wxWindow 派生的。这就是为什么sizer必须知道你想要删除子窗口。

另请参阅:http://docs.wxwidgets.org/2.8/wx_wxstatictext.htmlhttp://docs.wxwidgets.org/2.8/wx_wxtextctrl.htmlhttp://docs.wxwidgets.org/2.8/wx_wxbutton.html

干杯!