2

I'm trying to create an application that displays the contents of a QLabel (or a QTextEdit) by adding the letters one by one (as if the application were writing them).

This is an example in python:

import os, time

def write(text, time):
    base = ""
    for char in text:
        os.system("clear")
        base += char
        print base
        time.sleep(time)

def main():
    text = "this is a test"
    write(text, 0.05)

This is my PyQt code:

from PyQt4 import QtCore, QtGui
import sys

class Example(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.initUi()

        text = "this is a test"
        self.write(text, 50)

    def initUi(self):
        self.setGeometry(300, 300, 250, 150) 
        self.show()

        self.label = QtGui.QLabel(self)
        self.label.move(120, 60)

    def write(self, text, msec):
        base = ""
        for char in text:
            base += char
            self.label.setText(base)
            QtCore.QThread.msleep(msec)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

This code, obviously, doesn't work - but I have no idea how to do this "simple" thing.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
u34na
  • 33
  • 1
  • 3
  • have you tried adding a `self.label.repaint()` before each sleep? – Alex Martelli Dec 22 '14 at 19:14
  • I partially solved my problem by using processEvent. Unfortunately, what i really need is an sleep method that don't freeze the main window. This app must do the next things: display ("writing") "welcome" -> do some work -> clear the label contents -> display the results in the same label -> do other tasks -> etc. I hope someone can help me. Thanks! – u34na Dec 22 '14 at 21:05

1 Answers1

1

You can use QTimer to do this:

import sys
from PyQt4 import QtCore, QtGui

class Example(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.label = QtGui.QLabel(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.label)
        self._text = 'this is a test'
        self._index = 0
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.handleTimer)
        self.timer.start(200)

    def handleTimer(self):
        self._index += 1
        self.label.setText(self._text[:self._index])
        if self._index > len(self._text):
            self.timer.stop()

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.setGeometry(300, 300, 250, 150)
    ex.show()
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you ekhumoro! Like I said above, I have solved this issue with the _processEvents()_ method but my real problem is another one (I wasn't able to explain well what I need). Anyway, your code has been very useful to me and it is the right answer to the question I asked. So again, thanks for that. – u34na Dec 22 '14 at 23:37