I m able to dynamically add a layout to QHBoxLayout in pyqt , however I m unable to remove them once added.

Main aim is to dynamically add and remove a layout based on the Radio Button selected.

def SearchTab(self):

self.layout = QVBoxLayout()

button_layout = QHBoxLayout()
radio_button_1 = QRadioButton("Search")
radio_button_2 = QRadioButton("Update")
button_layout.addWidget(radio_button_1)
button_layout.addWidget(radio_button_2)
self.layout.addItem(button_layout)

radio_button_1.toggled.connect(lambda :self.SelectButtonCheck(radio_button_1))
radio_button_1.toggled.connect(lambda :self.UpdateButtonCheck(radio_button_2))

self.setTabText(0,"Search")

self.tab1.setLayout(self.layout)


def SelectButtonCheck(self,b):
if b.text() == "Search":
if b.isChecked():
print(b.text()+ "is selected")
self.pg_details = pgd.PGDetails()
layout = self.pg_details.returnLayout()
self.layout.addLayout(layout)


def UpdateButtonCheck(self,b):

if b.text() == "Update":
if b.isChecked():
print(b.text()+ " is selected")
for i in range(self.layout.count()):
print(self.layout.itemAt(i))
temp_layout = self.layout.itemAt(i)
widget = temp_layout.widget()
temp_layout.removeItem(temp_layout)

if widget is not None:
widget.deleteLater()


Initial Screen-

[![][1]][1]

Currently I m able to add the layout when "Search" Radio Button is selected --

[![Contains QFromLayout][2]][2]

But Nothing happens when I select "Update" RadioButton

[![Nothing Happens here and the application crashed][3]][3]


Also find the layouts that have been added-

for i in range(self.layout.count()):
print(self.layout.itemAt(i))
<PyQt5.QtWidgets.QHBoxLayout object at 0x1180ec438>
<PyQt5.QtWidgets.QFormLayout object at 0x1180ff828>

Layouts are being added but not getting removed.

Any leads would be helpful , in what I m missing here


[1]: iaRg2.jpg
[2]: LxpXc.jpg
[3]: OJJGy.jpg