我是PyQt的新手。我希望GUI从时间0到时间t1是GUI1,GUI2从时间t1到t2,...,GUIn从时间t(n-1)到t(n)。所以我想我必须使用多线程技术。有没有人有可用的代码与我的要求具有相同的功能?
此外,当我尝试将线程的完成信号连接到UpdateUI函数时遇到了一些问题。代码如下所示:
class Ui_MainWindow(QtCore.QObject):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(771, 586)
MainWindow.setMinimumSize(QtCore.QSize(771, 586))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
MainWindow.setFont(font)
#Feng
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.layoutWidget = QtGui.QWidget(self.centralWidget)
self.layoutWidget.setGeometry(QtCore.QRect(10, 500, 751, 27))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
self.horizontalLayout.setMargin(11)
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.btnSkip = QtGui.QPushButton(self.layoutWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btnSkip.sizePolicy().hasHeightForWidth())
self.btnSkip.setSizePolicy(sizePolicy)
self.btnSkip.setMinimumSize(QtCore.QSize(0, 23))
self.btnSkip.setFlat(False)
self.btnSkip.setObjectName(_fromUtf8("btnSkip"))
self.horizontalLayout.addWidget(self.btnSkip)
self.horizontalSlider = QtGui.QSlider(self.layoutWidget)
self.horizontalSlider.setMinimumSize(QtCore.QSize(0, 22))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName(_fromUtf8("horizontalSlider"))
self.horizontalLayout.addWidget(self.horizontalSlider)
self.btnPrevious = QtGui.QPushButton(self.layoutWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btnPrevious.sizePolicy().hasHeightForWidth())
self.btnPrevious.setSizePolicy(sizePolicy)
self.btnPrevious.setMinimumSize(QtCore.QSize(0, 23))
self.btnPrevious.setObjectName(_fromUtf8("btnPrevious"))
self.horizontalLayout.addWidget(self.btnPrevious)
self.btnNext = QtGui.QPushButton(self.layoutWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btnNext.sizePolicy().hasHeightForWidth())
self.btnNext.setSizePolicy(sizePolicy)
self.btnNext.setMinimumSize(QtCore.QSize(0, 23))
self.btnNext.setObjectName(_fromUtf8("btnNext"))
self.horizontalLayout.addWidget(self.btnNext)
self.layoutWidget1 = QtGui.QWidget(self.centralWidget)
self.layoutWidget1.setGeometry(QtCore.QRect(11, 11, 751, 171))
self.layoutWidget1.setObjectName(_fromUtf8("layoutWidget1"))
self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget1)
self.verticalLayout.setMargin(11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.textEditTaskName = QtGui.QTextEdit(self.layoutWidget1)
self.textEditTaskName.setEnabled(False)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.textEditTaskName.sizePolicy().hasHeightForWidth())
self.textEditTaskName.setSizePolicy(sizePolicy)
self.textEditTaskName.setMinimumSize(QtCore.QSize(0, 94))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.textEditTaskName.setFont(font)
self.textEditTaskName.setAcceptDrops(False)
self.textEditTaskName.setFrameShape(QtGui.QFrame.NoFrame)
#Feng
self.textEditTaskName.setText("Welcome")
self.textEditTaskName.setObjectName(_fromUtf8("textEditTaskName"))
self.verticalLayout.addWidget(self.textEditTaskName)
self.textEditTaskdesciption = QtGui.QTextEdit(self.layoutWidget1)
self.textEditTaskdesciption.setEnabled(False)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.textEditTaskdesciption.sizePolicy().hasHeightForWidth())
self.textEditTaskdesciption.setSizePolicy(sizePolicy)
self.textEditTaskdesciption.setMinimumSize(QtCore.QSize(0, 69))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(12)
self.textEditTaskdesciption.setFont(font)
self.textEditTaskdesciption.setAcceptDrops(False)
#Feng
self.textEditTaskdesciption.setText('Welcome to my program.')
self.textEditTaskdesciption.setFrameShape(QtGui.QFrame.NoFrame)
self.textEditTaskdesciption.setFrameShadow(QtGui.QFrame.Plain)
self.textEditTaskdesciption.setObjectName(_fromUtf8("textEditTaskdesciption"))
self.verticalLayout.addWidget(self.textEditTaskdesciption)
self.layoutWidget2 = QtGui.QWidget(self.centralWidget)
self.layoutWidget2.setGeometry(QtCore.QRect(10, 190, 751, 311))
self.layoutWidget2.setObjectName(_fromUtf8("layoutWidget2"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.layoutWidget2)
self.verticalLayout_2.setMargin(11)
self.verticalLayout_2.setSpacing(6)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.labelTESTImage = QtGui.QLabel(self.layoutWidget2)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.labelTESTImage.setFont(font)
self.labelTESTImage.setAlignment(QtCore.Qt.AlignCenter)
image = QtGui.QImage('Images/deptLogo.png')
#Feng
self.labelTESTImage.setPixmap(QPixmap.fromImage(image))#("deptLogo.png"))
self.labelTESTImage.setScaledContents(True)
self.labelTESTImage.setObjectName(_fromUtf8("labelTESTImage"))
self.labelTESTImage.setScaledContents(True)
#Feng
self.btnNext.pressed.connect(self.nextSession)
self.btnPrevious.pressed.connect(self.previousSession)
#Feng
self.horizontalSlider.setRange(-1,len(strSettings)-1)
self.horizontalSlider.setValue(-1)
self.horizontalSlider.valueChanged.connect(self.dragSlider)
#self.btnNext.clicked.connect(self.readingSettingsFile)
#self.btnNext.clicked.connect(self.loadimage)
self.verticalLayout_2.addWidget(self.labelTESTImage)
self.progressBar = QtGui.QProgressBar(self.layoutWidget2)
self.progressBar.setMinimumSize(QtCore.QSize(0, 22))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.progressBar.setVisible(False)
self.verticalLayout_2.addWidget(self.progressBar)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 771, 21))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
self.menuFile = QtGui.QMenu(self.menuBar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
self.menuEdit = QtGui.QMenu(self.menuBar)
self.menuEdit.setObjectName(_fromUtf8("menuEdit"))
self.menuHelp = QtGui.QMenu(self.menuBar)
self.menuHelp.setObjectName(_fromUtf8("menuHelp"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.menuBar.addAction(self.menuFile.menuAction())
self.menuBar.addAction(self.menuEdit.menuAction())
self.menuBar.addAction(self.menuHelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "my test", None))
MainWindow.setWindowIcon(QtGui.QIcon('Images/test.ico'))
self.btnSkip.setText(_translate("MainWindow", "Skip", None))
self.btnPrevious.setText(_translate("MainWindow", "&Previous", None))
self.btnNext.setText(_translate("MainWindow", "&Next", None))
#self.labelTESTImage.setText(_translate("MainWindow", "my test", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.menuEdit.setTitle(_translate("MainWindow", "Edit", None))
self.menuHelp.setTitle(_translate("MainWindow", "Help", None))
#Feng
def dragSlider(self):
global strSettings, timeSleep, lineCurrent
lineCurrent = int(self.horizontalSlider.value())
if (lineCurrent != -1):
line = strSettings[lineCurrent]
wavFilename, showProgressBar, valPauseTime, valName, valDescription, valImageLoad = line.split("\t")
self.time_thread = timerThread(float(valPauseTime))
self.connect(self.time_thread, SIGNAL("finished()"),self, self.updateUIFromFile())
else:
self.btnNext.setText(_translate("MainWindow", "&Start", None))
self.textEditTaskName.setText("Welcome")
self.textEditTaskdesciption.setText('This program is designed by Feng.')
self.labelOTCImage.setPixmap(QPixmap.fromImage(QtGui.QImage('Images/deptLogo.png')))
self.labelOTCImage.setScaledContents(True)
self.progressBar.setVisible(False)
def nextSession(self):
global strSettings, timeSleep, lineCurrent
if(lineCurrent<len(strSettings)-1):
lineCurrent += 1
self.horizontalSlider.setValue(lineCurrent)
#self.updateUIFromFile()
def previousSession(self):
global strSettings, timeSleep, lineCurrent
if lineCurrent>0:
lineCurrent -= 1
else:
lineCurrent = -1
self.horizontalSlider.setValue(lineCurrent)
#self.updateUIFromFile()
def updateUIFromFile(self):
self.btnNext.setText(_translate("MainWindow", "&Next", None))
line = strSettings[lineCurrent]
wavFilename, showProgressBar, valPauseTime, valName, valDescription, valImageLoad = line.split("\t")
self.textEditTaskName.setText(valName)
self.textEditTaskdesciption.setText(valDescription)
self.labelOTCImage.setPixmap(QPixmap.fromImage(QtGui.QImage('resources/'+valImageLoad.replace("\n", ""))))
self.labelOTCImage.setScaledContents(False)
self.progressBar.setVisible(int(showProgressBar))
self.progressBar.setValue(50)
这是线程类
class timerThread(QtCore.QThread):
def __init__(self,sleepTime):
QtCore.QThread.__init__(self)
self.sleepTime = sleepTime
def __del__(self):
self.wait()
def run(self):
time.sleep(self.sleepTime) # artificial time delay
主要
if __name__ == "__main__":
try:
fileSettings = open('protocol.txt','r')
except IOError as e:
print ("Unable to open file") #Does not exist OR no read permissions
sys.exit(0)
strSettings = fileSettings.readlines()
strSettings = list(filter(None, strSettings))
fileSettings.close()
timeSleep = 0
lineCurrent = -1
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
文件'protocol.txt'包含每个QtWidget的内容以及GUI何时更改内容的时间表
答案 0 :(得分:0)
您未正确使用Ui_MainWindow
课程。有关如何使用自动生成的UI
类,请参阅此answer。
您应该继承UI
类,或者将其设置为您自己的类之一的实例属性。
class MainWIndow(QtGui.QMainWIndow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
有时,人们不会继承,但会将其分配给班级的属性:
class MainWIndow(QtGui.QMainWIndow):
def __init__(self, parent=None):
QtGui.QMainWIndow.__init__(self, parent)
self.ui = Ui_MainWindow(self)
self.ui.setupUi(self)
答案 1 :(得分:0)
所以虽然没有人想给出这个问题的答案,但我想我在Update GUI at different time interval in QT中找到了我提出的另一个问题的答案。
我希望有人可以给我这个答案的pyqt版本,因为这是在C ++中。