Reputation: 89
How to remove widget from other class? I have code below and wanted to use the "Remove" button to remove "fig_view2" then add other widget later. The "Remove" button and the fig_view2 are from different classes. Tried to delete via a function below but error
def DeleteWidget(self):
Tab1.layoutC2.removeWidget(fig_view2)
Error message is "AttributeError: type object 'Tab1' has no attribute 'layoutC2'"
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
from PyQt5.QtWidgets import ( QApplication, QWidget,QVBoxLayout,QPushButton,QTabWidget,QMainWindow,QGridLayout,QCompleter,QScrollArea,QWidget)
from PyQt5.QtWebEngineWidgets import *
class Win(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100,100, 300,300)
self.GuiApp=App()
self.setCentralWidget(self.GuiApp)
self.show()
class Tab1(QWidget):
def __init__(self, parent=None):
super(Tab1, self).__init__(parent)
fig_view1 = QWebEngineView(self)
fig_view1.show()
fig_view2 = QWebEngineView(self)
fig_view2.show()
layoutC2 = QGridLayout()
layoutC2.removeWidget(fig_view1)
layoutC2.addWidget(fig_view1,0,0)
layoutC2.addWidget(fig_view2,0,1)
self.setLayout(layoutC2)
class App(QWidget):
def __init__(self):
super().__init__()
self.layout = QGridLayout(self)
self.setLayout(self.layout)
#Button
BT1 = QPushButton('Remove',self)
BT1.clicked.connect(self.DeleteWidget)
self.layout.addWidget(BT1, 0,0,1,1)
self.tab1 = Tab1(self)
self.scrollbar = QScrollArea(widgetResizable= True)
self.scrollbar.setWidget(self.tab1)
self.tabwidget = QTabWidget()
self.tabwidget.addTab(self.scrollbar,"Tab1")
self.layout.addWidget(self.tabwidget, 0,2,13,1)
def DeleteWidget(self):
Tab1.layoutC2.removeWidget(fig_view2)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Win()
sys.exit(app.exec_())
Upvotes: 1
Views: 593
Reputation: 244202
You should consider the following:
Each class must have the responsibility to manage its internal elements, and in this case the one that must eliminate its internal widget is the same class.
The removeWidget method does not delete the widget it only means that the layout will no longer handle its geometry, if you want to delete a widget use the deleteLater () method.
If you want to access a variable that is accessed from another place then you must do it through the attributes of the class.
class Tab1(QWidget):
def __init__(self, parent=None):
super(Tab1, self).__init__(parent)
self.fig_view1 = QWebEngineView()
self.fig_view2 = QWebEngineView()
self.lay = QGridLayout(self)
self.lay.addWidget(self.fig_view1, 0, 0)
self.lay.addWidget(self.fig_view2, 0, 1)
def removefig2(self):
if self.fig_view2 is not None:
self.lay.removeWidget(self.fig_view2)
self.fig_view2.deleteLater()
self.fig_view2 = None
class App(QWidget):
def __init__(self):
super().__init__()
# Button
button_bt1 = QPushButton("Remove")
self.lay = QGridLayout(self)
self.lay.addWidget(button_bt1, 0, 0, 1, 1)
self.tab1 = Tab1()
self.scrollbar = QScrollArea(widgetResizable=True)
self.scrollbar.setWidget(self.tab1)
self.tabwidget = QTabWidget()
self.tabwidget.addTab(self.scrollbar, "Tab1")
self.lay.addWidget(self.tabwidget, 0, 2, 13, 1)
button_bt1.clicked.connect(self.tab1.removefig2)
Upvotes: 3