Python Forum
[PyQt] remove widget from other class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] remove widget from other class
#1
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_())
Reply
#2
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_())
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to place global tk text widget in class or on canvas puje 1 2,321 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  [Tkinter] Displaying ComboBoxDialog Widget in a Class Vicolas 3 2,857 Feb-03-2019, 03:00 PM
Last Post: Larz60+
  [Tkinter] Class with text widget method AeranicusCascadia 3 7,785 Nov-14-2017, 11:33 PM
Last Post: AeranicusCascadia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020