[PyQt] Text "write on" effect

Wilbert Berendsen wbsoft at xs4all.nl
Tue Feb 28 07:54:44 GMT 2012

Op Mon, 27 Feb 2012 17:02:01 -0800 (PST)
JPolk <jpolk5001 at yahoo.com> schreef:

> okay, this may seem a bit superfluous, but would be a very nice
> animation effect to have in one's toolbox..
> For those that don't know what a "write on" animation is,...you
> probably see them on TV everyday,..
> but it's when a whole sentence is displayed a character at time, with
> just a fraction of a second delay
> in-between each character...
> Thought this would be trivial at first,...but there's something going
> on in Qt that senses there's about to
> be a bunch of fast updates, so it waits until everything is done and
> updates *once*, which defeats the
> purpose....
> Here's how the madness unfolds,..
> text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed
> diam nonummy nibh euismod tincidunt ut laoreet"
> accum = QString("")
>  for i in range(0, len(text)):
>          accum = accum + text[i]
>          self.label.setText(accum)
>          self.label.update()
>          sleep(.1)
> So, I accumulate the string char by char calling update() and sleep()
> at each interval,...a button triggers this function.
> But instead of seeing the effect progress as expected, it just
> display the whole string at once...
> Anybody got any ideas how to achieve this effect ?

the sleep function causes the main thread (GUI thread) to be blocked,
so the widget is not repainted. It only repaints when the for-loop has
terminated, and thus shows the full string at once.

A solution would be to use a timer that regularly updates the label text

Wilbert Berendsen

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: text/x-python
Size: 1306 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120228/462f518c/attachment.py>

More information about the PyQt mailing list