Search code examples
pythonpyqtqt4

stackedWidget using pyqt


Im new at Python, QT4 and pyqt and I can't get the widgets to change using setCurrentIndex. I'm sure I am not using it correctly, but here's my initial code. First is the pyqt code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Thu May  5 17:15:28 2016
#      by: PyQt4 UI code generator 4.9.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(400, 300)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.stackedWidget = QtGui.QStackedWidget(self.centralWidget)
        self.stackedWidget.setGeometry(QtCore.QRect(-41, -41, 451, 311))
        self.stackedWidget.setObjectName(_fromUtf8("stackedWidget"))
        self.page = QtGui.QWidget()
        self.page.setObjectName(_fromUtf8("page"))
        self.label = QtGui.QLabel(self.page)
        self.label.setGeometry(QtCore.QRect(180, 70, 50, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(self.page)
        self.pushButton.setGeometry(QtCore.QRect(170, 140, 80, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.stackedWidget.addWidget(self.page)
        self.page_2 = QtGui.QWidget()
        self.page_2.setObjectName(_fromUtf8("page_2"))
        self.label_2 = QtGui.QLabel(self.page_2)
        self.label_2.setGeometry(QtCore.QRect(200, 90, 50, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.stackedWidget.addWidget(self.page_2)
        MainWindow.setCentralWidget(self.centralWidget)

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "Page 1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Go to 2", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Page 2", None, QtGui.QApplication.UnicodeUTF8))

And here is the code I'm trying:

import sys from form import *

class MyDialog(QtGui.QMainWindow):
  def __init__(self, parent=None):
     QtGui.QWidget.__init__(self, parent)
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self)
     self.ui.pushButton.clicked.connect(self.Change)

  def Change(self):
     self.stackedWidget.setCurrentIndex(1)

if __name__ == "__main__":
   app = QtGui.QApplication(sys.argv)
   myapp = MyDialog()
   myapp.show()
   sys.exit(app.exec_())

When I push my button, I get the error"'MyDialog' object has no attribute 'stackedWidget'.

I'm sure this is easy to the experienced programmer.


Solution

  • The error message means exactly what it says: MyDialog doesn't create a member variable named self.stackedWidget, but it tries to access such a variable in Change, so an AttributeError is raised when you click the button. The variable you want is a member of self.ui, which is an instance of UI_MainWindow.

    The solution is to change the method Change to:

    def Change(self):
        self.ui.stackedWidget.setCurrentIndex(1)