1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
import sys
from PyQt4 import QtCore, QtGui
#############################################################################
# création d'une chaine de caractères au hasard
from random import randint
cars = u" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
lgcars = len(cars)
def creachaine():
# longueur du texte au hasard
lg = randint(0, 255)
# texte au hasard
return ''.join([cars[randint(0, lgcars-1)] for i in xrange(0, lg)])
#############################################################################
class Fenetre(QtGui.QWidget):
def __init__(self, parent=None):
super(Fenetre, self).__init__(parent)
self.resize(800, 600)
self.bouton = QtGui.QPushButton(u"Affichez!", self)
self.bouton.clicked.connect(self.lanceAffiche)
self.edit = QtGui.QTextEdit(self)
posit = QtGui.QGridLayout()
posit.addWidget(self.bouton, 0, 0)
posit.addWidget(self.edit, 1, 0)
self.setLayout(posit)
def lanceAffiche(self):
"""lance l'affichage du texte"""
for i in xrange(0, 1000):
ligne = creachaine() # création d'une chaine au hasard
self.affiche(u"%4d: %s" % (i+1, ligne))
def affiche(self, msg):
"""Ecrire dans le widget edit (QTextEdit)"""
# ajoute msg au texte existant
self.edit.append(msg)
# fait bouger le curseur à la fin du texte
self.edit.moveCursor(QtGui.QTextCursor.End,
QtGui.QTextCursor.MoveAnchor)
# force le rafraichissement pour affichage en temps réel
QtCore.QCoreApplication.processEvents()
#############################################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
fen = Fenetre()
fen.show()
sys.exit(app.exec_()) |
Partager