2016-05-10 15 views
0

好的,我試着創建一個勾號,這樣我可以顯示經過的時間並在一段時間後改變屏幕。 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)不工作,並給我一個錯誤。 我嘗試了很多東西,通常會遇到「自我」的麻煩。

我可以使用一些幫助和可能的一點諮詢。

+0

你想製作一個秒錶嗎? – danidee

+0

不是。我們用樹莓派製作嵌入式控件。有一段時間運行的輸出,屏幕將顯示剩餘時間。電機也有累計運行時間。 – dporada

+0

我來自嵌入式控制環境,通常帶有幾個指示燈或可能是LED顯示屏的LED。通常,我們爲主演的A/D讀數和更新io建立一箇中斷標記。這是計數和伊西斯計時器。定時器必須在後臺運行。 – dporada

回答

0

問題出在函數Change上,具體是行UI_MainWindow.setCurrentIndex(1)UI_MainWindow是一個類的名稱,你想要的是一個實例的類。您在MyDialog的__init__方法中創建了這樣一個實例;它是self.ui。因此,上面提到的這條線應該是self.ui.setCurrentIndex(1)。這就是實例self.ui上的方法setCurrentIndex

爲了記錄,Python中存在靜態方法和類方法,因此原始代碼的語法是完全有效的。在Python嘗試(並失敗)調用名爲UI_MainWindow.setCurrentIndex的類/靜態方法之前,您不會收到錯誤。這樣的方法確實出現在Qt中,例如QMessageBox.critical

+0

我已經嘗試過了。我得到的錯誤是「NameError:全局名稱'self'未定義」。將自變量添加到def中更改(self):給出錯誤TypeError:Change()只需要1個參數(0給定)。我不知道如何解決這個問題。 – dporada

+0

對不起,我錯過了'Change'方法聲明不正確的事實(它應該有一個參數,'self')。此外,在創建計時器後出現的函數調用應該是'timer.timeout.connect(myapp.Change)'。同樣,類和實例之間的混淆也是一樣的;你需要一個MyDialog的實例,而不是類本身。 –

相關問題