简体   繁体   English

使用pyqt的stackedWidget

[英]stackedWidget using pyqt

Im new at Python, QT4 and pyqt and I can't get the widgets to change using setCurrentIndex. 我是Python,QT4和pyqt的新手,我无法使用setCurrentIndex更改小部件。 I'm sure I am not using it correctly, but here's my initial code. 我确定我没有正确使用它,但这是我的初始代码。 First is the pyqt code: 首先是pyqt代码:

# -*- 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 * 从表格导入中导入sys *

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'. 当我按下按钮时,出现错误“ MyDialog”对象没有属性“ stackedWidget”。

I'm sure this is easy to the experienced programmer. 我相信这对有经验的程序员来说很容易。

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. 错误消息的含义与所声明的完全相同: MyDialog不会创建名为self.stackedWidget的成员变量,但会尝试在Change访问此类变量,因此单击按钮时会引发AttributeError。 The variable you want is a member of self.ui , which is an instance of UI_MainWindow . 您想要的变量是self.ui的成员, self.uiUI_MainWindow的实例。

The solution is to change the method Change to: 解决方法是将方法Change为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM