您好我是pyqt的新手,我正在尝试设计多页面GUI。我试图在一个页面中实现带有计时器的多页面GUI。我能够在单页GUI中更新计时器,但在下面提到的多页GUI中,计时器没有更新。如果我的怀疑是微不足道的,我道歉,但我请求帮助我,以便我能够提高我对设计更复杂系统的理解
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'page1.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_page1(object):
def setupUi(self, page1):
page1.setObjectName(_fromUtf8("page1"))
page1.resize(800, 600)
self.centralwidget = QtGui.QWidget(page1)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.go_to_p2 = QtGui.QPushButton(self.centralwidget)
self.go_to_p2.setGeometry(QtCore.QRect(40, 120, 75, 23))
self.go_to_p2.setObjectName(_fromUtf8("go_to_p2"))
page1.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(page1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
page1.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(page1)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
page1.setStatusBar(self.statusbar)
self.retranslateUi(page1)
QtCore.QMetaObject.connectSlotsByName(page1)
def retranslateUi(self, page1):
page1.setWindowTitle(_translate("page1", "MainWindow", None))
self.go_to_p2.setText(_translate("page1", "Go to Page 2", None))
class Ui_page2(object):
def setupUi(self, page2):
page2.setObjectName(_fromUtf8("page2"))
page2.resize(800, 600)
self.centralwidget = QtGui.QWidget(page2)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.X1 = QtGui.QPushButton(self.centralwidget)
self.X1.setGeometry(QtCore.QRect(30, 80, 75, 23))
self.X1.setObjectName(_fromUtf8("X1"))
self.X1.clicked.connect(self.doAction)
self.timer = QtCore.QBasicTimer()
self.step = 0
self.X2 = QtGui.QPushButton(self.centralwidget)
self.X2.setGeometry(QtCore.QRect(690, 80, 75, 23))
self.X2.setObjectName(_fromUtf8("X2"))
self.back = QtGui.QPushButton(self.centralwidget)
self.back.setGeometry(QtCore.QRect(510, 510, 75, 23))
self.back.setObjectName(_fromUtf8("back"))
self.lcdNumber_x1 = QtGui.QLCDNumber(self.centralwidget)
self.lcdNumber_x1.setGeometry(QtCore.QRect(30, 100, 64, 23))
self.lcdNumber_x1.setObjectName(_fromUtf8("lcdNumber_x1"))
self.lcdNumber_x2 = QtGui.QLCDNumber(self.centralwidget)
self.lcdNumber_x2.setGeometry(QtCore.QRect(700, 100, 64, 23))
self.lcdNumber_x2.setObjectName(_fromUtf8("lcdNumber_x2"))
page2.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(page2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
page2.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(page2)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
page2.setStatusBar(self.statusbar)
self.retranslateUi(page2)
QtCore.QMetaObject.connectSlotsByName(page2)
def retranslateUi(self, page2):
page2.setWindowTitle(_translate("page2", "MainWindow", None))
self.X1.setText(_translate("page2", "X1", None))
self.X2.setText(_translate("page2", "X2", None))
self.back.setText(_translate("page2", "Back", None))
def timerEvent(self, e):
if self.step >= 100:
self.timer.stop()
#self.X1.setText('Finished')
return
self.step = self.step + 1
time.sleep(1)
self.lcdNumber_x1.display(self.step)
def doAction(self):
if self.timer.isActive():
self.timer.stop()
self.X1.setText('X1 start')
else:
self.timer.start(1000, self.lcdNumber_x1)
self.X1.setText('X1 stop')
def openWindow(self):
page1.hide()
page2.show()
def backWindow(self):
page2.hide()
page1.show()
if __name__ == "__main__":
import sys
import time
app = QtGui.QApplication(sys.argv)
page1 = QtGui.QMainWindow()
ui = Ui_page1()
ui.setupUi(page1)
ui.go_to_p2.clicked.connect(openWindow)
page1.show()
page2 = QtGui.QMainWindow()
ui = Ui_page2()
ui.setupUi(page2)
ui.back.clicked.connect(backWindow)
sys.exit(app.exec_())
提前感谢..
答案 0 :(得分:0)
您好用以下代码解决了这个问题..
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'page1.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_page1(object):
def setupUi(self, page1):
page1.setObjectName(_fromUtf8("page1"))
page1.resize(800, 600)
self.centralwidget = QtGui.QWidget(page1)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.go_to_p2 = QtGui.QPushButton(self.centralwidget)
self.go_to_p2.setGeometry(QtCore.QRect(40, 120, 75, 23))
self.go_to_p2.setObjectName(_fromUtf8("go_to_p2"))
page1.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(page1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
page1.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(page1)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
page1.setStatusBar(self.statusbar)
self.retranslateUi(page1)
QtCore.QMetaObject.connectSlotsByName(page1)
def retranslateUi(self, page1):
page1.setWindowTitle(_translate("page1", "MainWindow", None))
self.go_to_p2.setText(_translate("page1", "Go to Page 2", None))
class Ui_page2(object):
def setupUi(self, page2):
page2.setObjectName(_fromUtf8("page2"))
page2.resize(800, 600)
self.centralwidget = QtGui.QWidget(page2)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.X1 = QtGui.QPushButton(self.centralwidget)
self.X1.setGeometry(QtCore.QRect(30, 80, 75, 23))
self.X1.setObjectName(_fromUtf8("X1"))
self.X2 = QtGui.QPushButton(self.centralwidget)
self.X2.setGeometry(QtCore.QRect(690, 80, 75, 23))
self.X2.setObjectName(_fromUtf8("X2"))
self.back = QtGui.QPushButton(self.centralwidget)
self.back.setGeometry(QtCore.QRect(510, 510, 75, 23))
self.back.setObjectName(_fromUtf8("back"))
self.lcdNumber_x1 = QtGui.QLCDNumber(self.centralwidget)
self.lcdNumber_x1.setGeometry(QtCore.QRect(30, 100, 64, 23))
self.lcdNumber_x1.setObjectName(_fromUtf8("lcdNumber_x1"))
self.lcdNumber_x2 = QtGui.QLCDNumber(self.centralwidget)
self.lcdNumber_x2.setGeometry(QtCore.QRect(700, 100, 64, 23))
self.lcdNumber_x2.setObjectName(_fromUtf8("lcdNumber_x2"))
page2.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(page2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
page2.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(page2)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
page2.setStatusBar(self.statusbar)
self.retranslateUi(page2)
QtCore.QMetaObject.connectSlotsByName(page2)
def retranslateUi(self, page2):
page2.setWindowTitle(_translate("page2", "MainWindow", None))
self.X1.setText(_translate("page2", "X1", None))
self.X2.setText(_translate("page2", "X2", None))
self.back.setText(_translate("page2", "Back", None))
class MainWindow(QtGui.QMainWindow, Ui_page2):
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.X1.clicked.connect(self.doAction)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.on_timeout)
self.step = 0
def on_timeout(self):
if self.step >= 5:
self.timer.stop()
self.X1.setText('Finished')
#self.back.clicked.connect(backWindow)
return
self.step = self.step + 1
self.lcdNumber_x1.display(self.step)
def doAction(self):
if self.timer.isActive():
self.timer.stop()
self.X1.setText('Start')
else:
self.timer.start(1000)
self.X1.setText('Stop')
def openWindow(self):
page1.close()
w.show()
#self.hide()
#sys.exit(app.exec_())
def backWindow(self):
w.hide()
page1.show()
#sys.exit(app.exec_())
if __name__ == "__main__":
import sys
import time
app = QtGui.QApplication(sys.argv)
w= MainWindow()
page1 = QtGui.QMainWindow()
ui = Ui_page1()
ui.setupUi(page1)
ui.go_to_p2.clicked.connect(openWindow)
page1.show()
#w= MainWindow()
#w.show()
w.back.clicked.connect(backWindow)
sys.exit(app.exec_())
#w.back.clicked.connect(backWindow)
#w.show()
however i have an# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'page1.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_page1(object):
def setupUi(self, page1):
page1.setObjectName(_fromUtf8("page1"))
page1.resize(800, 600)
self.centralwidget = QtGui.QWidget(page1)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.go_to_p2 = QtGui.QPushButton(self.centralwidget)
self.go_to_p2.setGeometry(QtCore.QRect(40, 120, 75, 23))
self.go_to_p2.setObjectName(_fromUtf8("go_to_p2"))
page1.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(page1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
page1.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(page1)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
page1.setStatusBar(self.statusbar)
self.retranslateUi(page1)
QtCore.QMetaObject.connectSlotsByName(page1)
def retranslateUi(self, page1):
page1.setWindowTitle(_translate("page1", "MainWindow", None))
self.go_to_p2.setText(_translate("page1", "Go to Page 2", None))
class Ui_page2(object):
def setupUi(self, page2):
page2.setObjectName(_fromUtf8("page2"))
page2.resize(800, 600)
self.centralwidget = QtGui.QWidget(page2)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.X1 = QtGui.QPushButton(self.centralwidget)
self.X1.setGeometry(QtCore.QRect(30, 80, 75, 23))
self.X1.setObjectName(_fromUtf8("X1"))
self.X2 = QtGui.QPushButton(self.centralwidget)
self.X2.setGeometry(QtCore.QRect(690, 80, 75, 23))
self.X2.setObjectName(_fromUtf8("X2"))
self.back = QtGui.QPushButton(self.centralwidget)
self.back.setGeometry(QtCore.QRect(510, 510, 75, 23))
self.back.setObjectName(_fromUtf8("back"))
self.lcdNumber_x1 = QtGui.QLCDNumber(self.centralwidget)
self.lcdNumber_x1.setGeometry(QtCore.QRect(30, 100, 64, 23))
self.lcdNumber_x1.setObjectName(_fromUtf8("lcdNumber_x1"))
self.lcdNumber_x2 = QtGui.QLCDNumber(self.centralwidget)
self.lcdNumber_x2.setGeometry(QtCore.QRect(700, 100, 64, 23))
self.lcdNumber_x2.setObjectName(_fromUtf8("lcdNumber_x2"))
page2.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(page2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
page2.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(page2)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
page2.setStatusBar(self.statusbar)
self.retranslateUi(page2)
QtCore.QMetaObject.connectSlotsByName(page2)
def retranslateUi(self, page2):
page2.setWindowTitle(_translate("page2", "MainWindow", None))
self.X1.setText(_translate("page2", "X1", None))
self.X2.setText(_translate("page2", "X2", None))
self.back.setText(_translate("page2", "Back", None))
class MainWindow(QtGui.QMainWindow, Ui_page2):
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.X1.clicked.connect(self.doAction)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.on_timeout)
self.step = 0
def on_timeout(self):
if self.step >= 5:
self.timer.stop()
self.X1.setText('Finished')
#self.back.clicked.connect(backWindow)
return
self.step = self.step + 1
self.lcdNumber_x1.display(self.step)
def doAction(self):
if self.timer.isActive():
self.timer.stop()
self.X1.setText('Start')
else:
self.timer.start(1000)
self.X1.setText('Stop')
def openWindow(self):
page1.close()
w.show()
#self.hide()
#sys.exit(app.exec_())
def backWindow(self):
w.hide()
page1.show()
#sys.exit(app.exec_())
if __name__ == "__main__":
import sys
import time
app = QtGui.QApplication(sys.argv)
w= MainWindow()
page1 = QtGui.QMainWindow()
ui = Ui_page1()
ui.setupUi(page1)
ui.go_to_p2.clicked.connect(openWindow)
page1.show()
#w= MainWindow()
#w.show()
w.back.clicked.connect(backWindow)
sys.exit(app.exec_())
#w.back.clicked.connect(backWindow)
#w.show()