无法在wxpython中完全从GridBagSizer中删除行

时间:2017-07-06 19:23:23

标签: wxpython

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

    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

1 个答案:

答案 0 :(得分: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()