Aug-04-2020, 05:03 PM
How to remove widget from other class?
I have code below and wanted to use the "Remove" button to remove "fig_view2"??
the button and the fig_view2 are from different classes.
I have code below and wanted to use the "Remove" button to remove "fig_view2"??
the button and the fig_view2 are from different classes.
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_())