【发布时间】:2016-10-04 12:12:15
【问题描述】:
我正在用 Python 构建一个简单的快速阅读器作为练习。基本上是一个Spritz 克隆。
运行 printword() 函数后,停止它的正确方法是什么?我应该把它放在一个线程中吗?使用线程还是 QThread?我有点迷茫。
#!/usr/bin/env python3
from time import sleep
import sys
from PyQt4 import QtGui, QtCore
class Fastreader(QtGui.QWidget):
def __init__(self):
super(Fastreader, self).__init__()
self.initUI()
def initUI(self):
self.le = QtGui.QLineEdit(self)
self.le.setAlignment(QtCore.Qt.AlignCenter)
self.le.move(20, 20)
self.btn = QtGui.QPushButton('Play', self)
self.btn.move(20, 50)
self.btn.clicked.connect(self.printword)
self.btn = QtGui.QPushButton('Stop', self)
self.btn.move(120, 50)
self.setGeometry(300, 300, 225, 80)
self.setWindowTitle('fastreader')
self.show()
def printword(self):
cb = QtGui.QApplication.clipboard()
text = cb.text()
words_per_minute = 200
sleeptime = 60.0/words_per_minute
for word in text.split(" "):
self.le.setText(word)
self.le.repaint()
sleep(sleeptime)
def main():
app = QtGui.QApplication(sys.argv)
ex = Fastreader()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
非常感谢!
【问题讨论】:
标签: python multithreading pyqt pyqt4