Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法清除wxBoxSizer的内容_Python_Wxpython_Wxwidgets - Fatal编程技术网

Python 无法清除wxBoxSizer的内容

Python 无法清除wxBoxSizer的内容,python,wxpython,wxwidgets,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,

由于某些原因,我的
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()
使用sizer.Clear(True)而不是sizer.Clear(),它应该可以工作

解释

如果发送到Clear()方法的参数为True,“那么子窗口也将被删除”()

您希望清除wxSizer的子项:wxStaticText、WXTEXTCRL、wxButton,它们都是从wxControl和wxWindow派生的。这就是为什么sizer必须知道您还想删除子窗口

另见:、和

干杯