I'm building a simple speed reader in Python as an exercise. Basically a Spritz clone.
What's the correct way to stop the printword() function once I run it? Should I put it in a thread? With threading or QThread? I'm a bit lost.
#!/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()
Thanks a bunch!