I'm use pyqt4,this is my code
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class View(QWidget):
def __init__(self):
super(View, self).__init__()
self.hbox = QHBoxLayout()
self.study_box = QVBoxLayout()
self.study_box.addWidget(QLabel('dsadasdasd'))
self.hbox.addLayout(self.study_box)
self.setLayout(self.hbox)
class Model(QWidget):
def __init__(self):
super(Model, self).__init__()
def func(self):
Tab().add_tab('sd')
class Tab(QTabWidget):
_instance = None
def __new__(cls, *args, **kw):
if cls._instance is None:
cls._instance = QWidget.__new__(cls, *args, **kw)
return cls._instance
def __init__(self):
super(Tab, self).__init__()
label = QPushButton('asd')
self.addTab(label, 'asd')
#method one, added successfully
# label.clicked.connect(lambda: self.add_tab('dsd'))
#method two, added unsuccessfully
label.clicked.connect(lambda: Model().func())
def add_tab(self, data):
# for x in range(self.count()):
# self.removeTab(0)
self.addTab(View(), data)
self.addTab(View(), data)
self.addTab(View(), data)
self.setCurrentIndex(1)
class Demo(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(0, 25, 2500, 1500)
wg = QWidget()
hbox = QHBoxLayout()
hbox.addWidget(Tab())
wg.setLayout(hbox)
self.setCentralWidget(wg)
self.setWindowState(Qt.WindowMaximized)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())
My requirement is that when I click the button of the first tab, he should add a tab to the interface. However, it does not have it. I am very confused. It clearly calls the add_tab function, but it does not add it to the interface. The structure of the class cannot be changed, because this is my code in the project, how do I get the implementation requirements?