2017-07-06 17 views
0

我想动态地删除GridBagSizer中一行中的一组按钮,然后重新使用单元格空间来生成一组新的按钮,但是当我尝试添加新的按钮到新删除的行,它说我无法做到,因为有一个项目已经在那个位置。无法完全从GridBagSizer中删除行wxpython

def delete_tool(self, event, specific_option=None): 

     for i in range(0 , 7): 
      item = self.activetoolsizer.FindItemAtPosition((specific_option, i)) 
      item.Show(False) 
      self.activetoolsizer.Layout() 
     self.activetoolcount -= 1 

回答

0

代替item.Show(False)这仅仅是隐藏的项目,你需要使用哪个item.Destroy()删除它。
稍后,您可以self.activetoolsizer.Add(......)在该位置,并记得使用self.activetoolsizer.Layout()

傻例子(继续点击按钮1和按钮2将被去除,然后更换):

import wx 

class MyFrame(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, title) 
     self.button1 = wx.Button(self,-1, "Button 1") 
     self.button2 = wx.Button(self,-1, "Button 2") 
     self.sizer = wx.GridBagSizer(2, 2) 
     self.sizer.Add(wx.StaticText(self,-1, "Label 1"), (0, 0), flag=wx.ALIGN_CENTER) 
     self.sizer.Add(self.button1, (0, 1), flag=wx.EXPAND) 
     self.sizer.Add(self.button2, (1, 0), flag=wx.EXPAND) 
     self.sizer.Add (wx.StaticText(self,-1, "Label 2"), (1, 1), flag=wx.ALIGN_CENTER) 
     self.Bind(wx.EVT_BUTTON, self.button_click, self.button1) 
     self.SetSizerAndFit(self.sizer) 
     self.button_index = 3 

    def button_click(self, event): 
     item = self.sizer.FindItemAtPosition((1, 0)) 
     if (item != None): 
      self.button2.Destroy() 
     else: 
      self.button2 = wx.Button(self,-1, "Button "+str(self.button_index)) 
      self.sizer.Add(self.button2, (1, 0), flag=wx.EXPAND) 
      self.sizer.Layout() 
      self.button_index +=1 

class MyApp(wx.App): 
    def OnInit(self): 
     frame = MyFrame(None, -1, "Replace item in Gridbagsizer") 
     frame.Show(True) 
     return True 

app = MyApp() 
app.MainLoop()