"QStackedWidget.setCurrentIndex": It does not work or error mark
<p>I'm doing a program with graphical interface using PyQt5 . I want to do is that when the user presses certain button, this change widget and show other options.</p>

<p>For this I decided to use QStackedWidget, and all my interface build it from the QT5 designer.</p>

<p>However, in my code, wanting to determine that my name button "btfr" show me "page_2" of my stackedWidget when pressed, using the QStackedWidget.setCurrentIndex method, this does nothing or make any error.</p>

<p>the code is as follows:</p>

<pre><code>import sys
from PyQt5 import uic
from PyQt5.QtCore import QTimeLine
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

infz = uic.loadUiType("main.ui")[0]


class FaderWidget(QWidget):

    def __init__(self, old_widget, new_widget):

        QWidget.__init__(self, new_widget)

        self.old_pixmap = QPixmap(new_widget.size())
        old_widget.render(self.old_pixmap)
        self.pixmap_opacity = 1.0
        self.timeline = QTimeLine()
        self.timeline.valueChanged.connect(self.animate)
        self.timeline.finished.connect(self.close)
        self.timeline.setDuration(333)
        self.timeline.start()
        self.resize(new_widget.size())
        self.show()

    def paintEvent(self, event):

        painter = QPainter()
        painter.begin(self)
        painter.setOpacity(self.pixmap_opacity)
        painter.drawPixmap(0, 0, self.old_pixmap)
        painter.end()

    def animate(self, value):

        self.pixmap_opacity = 1.0 - value
        self.repaint()

class StackedWidget(QStackedWidget):

    def __init__(self, parent=None):
        QStackedWidget.__init__(self, parent)

    def setCurrentIndex(self, index):
        self.stack = MyWindowClass()
        self.a = self.stack.stackedWidget.currentWidget()
        self.b = self.stack.stackedWidget.widget(index)
        self.fader_widget = FaderWidget(self.a, self.b)
        QStackedWidget.setCurrentIndex(self, index)
        print(self, index)

    def setPage1(self):
        self.setCurrentIndex(0)

    def setPage2(self):
        self.setCurrentIndex(1)


class MyWindowClass(QStackedWidget, infz):
    def __init__(self, parent=None):
        global pos, c, f
        self.pos = 0
        self.c = []
        self.f = False
        QStackedWidget.__init__(self, parent)
        self.setupUi(self)
        self.setWindowTitle('SkR')

if __name__ == "__main__":

    app = QApplication(sys.argv)

    window = MyWindowClass()
    window.resize(788, 518)
    stack = StackedWidget()
    window.btfr.clicked.connect(stack.setPage2)
    window.btnpx.clicked.connect(stack.setPage1)
    window.show()

    sys.exit(app.exec_())
</code></pre>

<p>What I intend with this code is that the change of widget does so with an effect: "fade out".</p>

<p>If I print the "self " and the "index " receiving QStackedWidget.setCurrentIndex shows the following:</p>

<pre><code>&lt;__main__.StackedWidget object at 0x7fc2eb6b5c18&gt; 0
</code></pre>

<p>The number zero is index, and the other element is self</p>

<p>Thank you for your attention, I hope someone can help.</p>

<p>Your question isn't completely clear, but don't you just want:</p>

<pre><code>def setIndex(self, index):
    self.setCurrentIndex(index)
</code></pre>

<p>However, this is a little redundant as you should able to link the button directly to the <code>setCurrentIndex</code> method and use <code>lambda</code> to pass the index value:</p>

<pre><code>btfr.clicked.connect(lambda: self.setCurrentIndex(2))
</code></pre>

python python-3.x pyqt pyqt5 qstackedwidget