Op Mon, 27 Feb 2012 17:02:01 -0800 (PST) JPolk <jpolk5...@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 (attached). -- Wilbert Berendsen (http://www.wilbertberendsen.nl)
#!/usr/bin/env python import os import sys from PyQt4 import QtCore, QtGui, uic from PyQt4.QtCore import ( QString ) class BasicWindow(QtGui.QMainWindow): def __init__(self, parent=None): super(BasicWindow, self).__init__(parent) #self.setupUi(self) self.label = QtGui.QLabel(self) self.label.setGeometry(QtCore.QRect(73, 110, 574, 331)) self.pushButton = QtGui.QPushButton(self) self.pushButton.setGeometry(QtCore.QRect(66, 30, 75, 27)) self.pushButton.setText("DoIt") scrX = 2048 scrY = 1150 winX = 720 winY = 530 pX = (scrX - winX) / 2 pY = (scrY - winY) / 2 self.setGeometry(pX, pY, winX, winY) self.label.setText('') self.pushButton.clicked.connect(self.DoIt) self.timer = QtCore.QTimer(interval=50, timeout=self.timeout) def DoIt(self): self.textGen = self.generateText() self.timer.start() def generateText(self): text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna" for i in range(len(text)): yield text[:i] def timeout(self): for accum in self.textGen: self.label.setText(accum) return self.timer.stop() def main(): app = QtGui.QApplication(sys.argv) form = BasicWindow() form.show() app.exec_() if __name__ == '__main__': main()
_______________________________________________ PyQt mailing list PyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt