Issue
I have prepared a little script to illustrate my problem. In fact I would like the button contained in tab 1 (Tab 1) to show me tab 4 (Tab 4). The QTabWidget is contained in the main class (CLASSE_Main) and I'm calling it from class 1 (CLASSE_1). I did some testing but nothing works.
Here is the script:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports PyQt5 -----------------------------------------------------------------------
from PyQt5.QtWidgets import (QWidget, QGroupBox, QGridLayout, QVBoxLayout, QMainWindow,
QTabWidget, QWidget, QApplication, QLabel, QPushButton)
# -------------------------------------------------------------------------------------
import sys
class CLASSE_1(QWidget) :
""" """
def __init__(self, parent=None):
super(CLASSE_1, self).__init__(parent)
label_classe_1 = QLabel("I'm class 1 label")
bouton_affichage_tab_4 = QPushButton("setCurrentIndex Tab 4")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_1, 0, 0)
grid_1.addWidget(bouton_affichage_tab_4, 0, 1)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
# Signal
bouton_affichage_tab_4.clicked.connect(self.show_tab_4)
def show_tab_4(self) :
""" """
from . import CLASSE_Main
cp = CLASSE_Main()
print("dir(cp)", dir(cp))
#cp.tab_widget.setCurrentIndex(3)
print("cp.get_tabwidget(), type(cp.get_tabwidget())", cp.get_tabwidget(), type(cp.get_tabwidget()))
# --------------------------------------
cp.get_tabwidget().setCurrentIndex(3)
# --------------------------------------
for cle, valeur in cp.__dict__.items():
if cle == 'tab_widget' :
print("valeur :", type(valeur))
# --------------------------------------
valeur.setCurrentIndex(3)
# --------------------------------------
class CLASSE_2(QWidget) :
""" """
def __init__(self, parent):
super(CLASSE_2, self).__init__(parent)
label_classe_2 = QLabel("I'm class 2 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_2, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_3(QWidget) :
""" """
def __init__(self, parent=None):
super(CLASSE_3, self).__init__(parent)
label_classe_3 = QLabel("I'm class 3 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_3, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_4(QWidget) :
""" """
def __init__(self, parent):
super(CLASSE_4, self).__init__(parent)
label_classe_4 = QLabel("I'm class 4 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_4, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_5(QWidget) :
""" """
def __init__(self, parent=None):
super(CLASSE_5, self).__init__(parent)
label_classe_5 = QLabel("I'm class 5 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_5, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_6(QWidget) :
""" """
def __init__(self, parent):
super(CLASSE_6, self).__init__(parent)
label_classe_6 = QLabel("I'm class 6 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_6, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_Main(QMainWindow):
""" Main class """
def __init__(self):
super(CLASSE_Main, self).__init__()
self.setWindowTitle('Help me please !')
self.setGeometry(20, 40, 600, 400)
self.setMinimumSize(600, 460)
self.tab_widget = QTabWidget()
self.win_widget_1 = CLASSE_1(self)
self.win_widget_2 = CLASSE_2(self)
self.win_widget_3 = CLASSE_3(self)
self.win_widget_4 = CLASSE_4(self)
self.win_widget_5 = CLASSE_5(self)
self.win_widget_6 = CLASSE_6(self)
widget = QWidget()
layout = QVBoxLayout(widget)
self.tab_widget.addTab(self.win_widget_1, "Tab 1")
self.tab_widget.addTab(self.win_widget_2, "Tab 2")
self.tab_widget.addTab(self.win_widget_3, "Tab 3")
self.tab_widget.addTab(self.win_widget_4, "Tab 4")
self.tab_widget.addTab(self.win_widget_5, "Tab 5")
self.tab_widget.addTab(self.win_widget_6, "Tab 6")
self.tab_widget.setStyleSheet("""QTabWidget::tab-bar {alignment: center;}""")
layout.addWidget(self.tab_widget)
self.setCentralWidget(widget)
def get_tabwidget(self) :
""" """
return self.tab_widget
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = CLASSE_Main()
ex.show()
sys.exit(app.exec_())
Solution
Explanation:
It is not necessary to import a class defined in the same script so remove from . import CLASSE_Main
.
On the other hand, although the code no longer throws the exception, you have another problem: "cp" is not "ex" since they are 2 different objects.
Solution:
One possible solution is to create a signal that sends the information of the new index that you want to show in the QTabWidget and link that signal to the setCurrentIndex method of the correct QTabWidget:
from PyQt5.QtCore import pyqtSignal
class CLASSE_1(QWidget):
customSignal = pyqtSignal(int)
# ...
def show_tab_4(self):
self.customSignal.emit(3)
class CLASSE_Main(QMainWindow):
""" Main class """
def __init__(self):
super(CLASSE_Main, self).__init__()
# ...
self.setCentralWidget(widget)
self.win_widget_1.customSignal.connect(self.tab_widget.setCurrentIndex)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.