【发布时间】:2015-08-12 11:35:42
【问题描述】:
我想在启动某些操作时向我的 GUI 添加一个 throbber。
这是我的脚本:
class StartTask(QtCore.QThread):
taskStarted = pyqtSignal()
def run(self):
self.taskStarted.emit()
class StopTask(QtCore.QThread):
taskStopped = pyqtSignal()
def run(self):
self.taskStopped.emit()
class Projet(object):
def __init__(self):
self.movie = '' # throbber
self.startTask = StartTask()
self.startTask.taskStarted.connect(self.startThrobber)
self.stopTask = StopTask()
self.stopTask.taskStopped.connect(self.stopThrobber)
def startThrobber(self):
# set up the movie screen on a label
self.movie_screen = QLabel()
# expand and center the label
main_layout = QVBoxLayout()
main_layout.addWidget(self.movie_screen)
ui.throbberTab2.setLayout(main_layout)
# use an animated gif file you have in the working folder
byteF = QByteArray()
movie = QMovie("D:\Various\Images\loader.gif", byteF)
movie.setCacheMode(QMovie.CacheAll)
movie.setSpeed(100)
self.movie_screen.setMovie(movie)
movie.start()
return movie
def stopThrobber(self):
movie1 = self.startThrobber()
movie1.stop()
def goAction(self):
if ui.chkbox.isChecked():
self.startTask.taskStarted.connect(self.startThrobber)
os.system(r'..........') # script launched
self.stopTask.taskStopped.connect(self.stopThrobber)
QMessageBox.information(self.popup(), "Information", "It works!")
由于我是第一次使用线程,所以我找不到哪里出了问题,哪里出了问题..
这没有结果,即使我认为我离正确的代码并不太远。
我已经设法让 throbber 出现,但不是在正确的时刻(当时线程不工作)。
【问题讨论】:
-
你为什么要使用线程?似乎什么都不需要线程。您创建的线程在启动时仅发出一个信号,仅此而已(线程结束)。线程用于长时间运行的后台任务,这似乎不是您在这里所做的。
-
好的。那么如何使用颤动器呢?因为它不是在脚本运行时设置的,它仅在脚本运行完成时才打开。我认为有一种特殊的方法可以做到这一点?
标签: python user-interface pyqt qthread throbber