如何使用计时器更改窗口小部件索引?

时间:2016-05-10 22:37:45

标签: python pyqt qt4

好的,我尝试创建一个勾号,这样我就可以显示经过的时间并在一段时间后更改屏幕。 pyqt程序如下:

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

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Tue May 10 08:43:50 2016
#      by: PyQt4 UI code generator 4.9.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
import time

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

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.setEnabled(True)
        MainWindow.resize(800, 480)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.stackedWidget = QtGui.QStackedWidget(self.centralWidget)
        self.stackedWidget.setGeometry(QtCore.QRect(0, 0, 800, 480))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        self.stackedWidget.setPalette(palette)
        self.stackedWidget.setObjectName(_fromUtf8("stackedWidget"))
        self.page_1 = QtGui.QWidget()
        self.page_1.setObjectName(_fromUtf8("page_1"))
        self.Software_2 = QtGui.QLabel(self.page_1)
        self.Software_2.setGeometry(QtCore.QRect(550, 310, 241, 71))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("DejaVu Sans"))
        font.setPointSize(32)
        font.setBold(False)
        font.setWeight(50)
        self.Software_2.setFont(font)
        self.Software_2.setObjectName(_fromUtf8("Software_2"))
        self.model_2 = QtGui.QLabel(self.page_1)
        self.model_2.setGeometry(QtCore.QRect(90, 300, 171, 91))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("DejaVu Sans"))
        font.setPointSize(32)
        font.setBold(False)
        font.setWeight(50)
        self.model_2.setFont(font)
        self.model_2.setObjectName(_fromUtf8("model_2"))
        self.ModNum_2 = QtGui.QLabel(self.page_1)
        self.ModNum_2.setGeometry(QtCore.QRect(110, 370, 101, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("DejaVu Sans"))
        font.setPointSize(24)
        font.setBold(False)
        font.setWeight(50)
        self.ModNum_2.setFont(font)
        self.ModNum_2.setObjectName(_fromUtf8("ModNum_2"))
        self.logo_3 = QtGui.QLabel(self.page_1)
        self.logo_3.setGeometry(QtCore.QRect(100, 60, 571, 191))
        self.logo_3.setText(_fromUtf8(""))
        self.logo_3.setPixmap(QtGui.QPixmap(_fromUtf8("../../logo.ppm")))
        self.logo_3.setObjectName(_fromUtf8("logo_3"))
        self.SoftNum_2 = QtGui.QLabel(self.page_1)
        self.SoftNum_2.setGeometry(QtCore.QRect(570, 370, 171, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("DejaVu Sans"))
        font.setPointSize(24)
        self.SoftNum_2.setFont(font)
        self.SoftNum_2.setObjectName(_fromUtf8("SoftNum_2"))
        self.stackedWidget.addWidget(self.page_1)
        self.page_2 = QtGui.QWidget()
        self.page_2.setObjectName(_fromUtf8("page_2"))
        self.logo_2 = QtGui.QLabel(self.page_2)
        self.logo_2.setGeometry(QtCore.QRect(100, 60, 571, 191))
        self.logo_2.setText(_fromUtf8(""))
        self.logo_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../logo.ppm")))
        self.logo_2.setObjectName(_fromUtf8("logo_2"))
        self.pbElectro = QtGui.QPushButton(self.page_2)
        self.pbElectro.setGeometry(QtCore.QRect(70, 360, 171, 32))
        self.pbElectro.setObjectName(_fromUtf8("pbElectro"))
        self.pbWater = QtGui.QPushButton(self.page_2)
        self.pbWater.setGeometry(QtCore.QRect(350, 360, 131, 32))
        self.pbWater.setObjectName(_fromUtf8("pbWater"))
        self.pbMatain = QtGui.QPushButton(self.page_2)
        self.pbMatain.setGeometry(QtCore.QRect(572, 360, 181, 32))
        self.pbMatain.setObjectName(_fromUtf8("pbMatain"))
        self.stackedWidget.addWidget(self.page_2)
        MainWindow.setCentralWidget(self.centralWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.Software_2.setText(QtGui.QApplication.translate("MainWindow", "Software", None, QtGui.QApplication.UnicodeUTF8))
        self.model_2.setText(QtGui.QApplication.translate("MainWindow", "Model", None, QtGui.QApplication.UnicodeUTF8))
        self.ModNum_2.setText(QtGui.QApplication.translate("MainWindow", "3000", None, QtGui.QApplication.UnicodeUTF8))
        self.SoftNum_2.setText(QtGui.QApplication.translate("MainWindow", "V 0.00.01", None, QtGui.QApplication.UnicodeUTF8))
        self.pbElectro.setText(QtGui.QApplication.translate("MainWindow", "Electrostatic Filtration", None, QtGui.QApplication.UnicodeUTF8))
        self.pbWater.setText(QtGui.QApplication.translate("MainWindow", "Water Removal", None, QtGui.QApplication.UnicodeUTF8))
        self.pbMatain.setText(QtGui.QApplication.translate("MainWindow", "Maintenance Mode", None, QtGui.QApplication.UnicodeUTF8))

然后我的主程序是:

import sys
from form import *
import time

from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QApplication

global myapp
start = time.time()



class MyDialog(QtGui.QMainWindow):
      def __init__(self, parent=None):
          QtGui.QWidget.__init__(self, parent)
          self.ui = Ui_MainWindow()
          self.ui.setupUi(self)
      def Change():
            print('tick')
            Ui_MainWindow.setCurrentIndex(1)

    #self.ui.pushButton.clicked.connect(self.Change)

if __name__ == "__main__":
      app = QtGui.QApplication(sys.argv)
      myapp = MyDialog()
      myapp.show()
      timer = QTimer()
      timer.timeout.connect(MyDialog.Change)
      timer.start(1000)
      sys.exit(app.exec_())

Ui_MainWindow.setCurrentIndex(1)行不起作用并给我一个错误。 我尝试过很多东西,通常会遇到“自我”的麻烦。

我可以使用一些帮助,可能还需要一些咨询。

1 个答案:

答案 0 :(得分:0)

问题在于函数Change,特别是行UI_MainWindow.setCurrentIndex(1)UI_MainWindow是类的名称,您想要的是类的实例。您在MyDialog的__init__方法中创建了这样一个实例;它是self.ui。因此,我上面提到的这条线应该是self.ui.setCurrentIndex(1)。这会调用实例setCurrentIndex上的方法self.ui

对于记录,Python中有静态方法和类方法之类的东西,因此原始代码的语法完全有效。在Python尝试(并且失败)调用名为UI_MainWindow.setCurrentIndex的类/静态方法之前,您不会收到错误。这些方法确实出现在Qt中,例如QMessageBox.critical