Python Forum
[PyQt] remove widget from other class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] remove widget from other class (/thread-28818.html)



remove widget from other class - issac_n - Aug-04-2020

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.

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_())



RE: remove widget from other class - Axel_Erfurt - Aug-04-2020

use self in class Tab1

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)
        self.fig_view1 = QWebEngineView()
        self.fig_view1.show()
        self.fig_view2 = QWebEngineView()
        self.fig_view2.show()
        self.layoutC2 = QGridLayout()
        self.layoutC2.removeWidget(self.fig_view1)
        self.layoutC2.addWidget(self.fig_view1,0,0)
        self.layoutC2.addWidget(self.fig_view2,0,1)
         
        self.setLayout(self.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):
        self.tab1.layoutC2.removeWidget(self.tab1.fig_view2)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Win()
    sys.exit(app.exec_())



RE: remove widget from other class - deanhystad - Aug-05-2020

It is bad form to mess around with another object's instance variables, but people do it all the time in Python. So you could do something like Axel is doing, or you could add a method to class Tab1 for doing this. I prefer the latter as the method documents that removing fig_view2 from Tab1 is part of the Tab1 interface. It also hides implementation details behind the Tab1 interface so App doesn't have to know about layout managers in a child widget.
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)

    def delete_fig_view(self):
        self.layoutC2.removeWidget(fig_view2)

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QGridLayout(self)
        self.setLayout(self.layout)
        self.tab1 = Tab1(self)

        #Button
        BT1 = QPushButton('Remove',self)
        BT1.clicked.connect(tab1.delete_fig_view)
        self.layout.addWidget(BT1, 0,0,1,1)
         
        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)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Win()
    sys.exit(app.exec_())
You should be asking yourself if you really want to delete the figure. You can delete and add widgets to a GUI, but I try to reuse what I have. What takes it's place? Is there forever a hole in Tab1? If so, is that a good idea?