Answer the question
In order to leave comments, you need to log in
Display generated objects on the fly
How to implement (pyqt) display of generated objects (drawEllipse) at the time of their creation (on the fly)? This code draws objects in memory and then displays an image:
import sys
import random
from PyQt4 import QtGui, QtCore
width=800
height=600
class DrawExample(QtGui.QWidget):
def __init__(self):
super(DrawExample, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, width, height)
self.setWindowTitle('Draw')
self.show()
def paintEvent(self, e):
qp = QtGui.QPainter()
qp.begin(self)
for i in range(10000):
x = random.randint(1, width-1)
y = random.randint(1, height-1)
color = QtGui.QColor(250,100,100)
qp.setPen(color)
qp.drawEllipse(x-3, y-3, 6, 6)
qp.end()
def main():
app = QtGui.QApplication(sys.argv)
ex = DrawExample()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Answer the question
In order to leave comments, you need to log in
#!/usr/bin/env python
import sys
import random
from PyQt4 import QtCore, QtGui
WIDTH = 800
HEIGHT = 600
class DrawExample(QtGui.QWidget):
def __init__(self):
super(DrawExample, self).__init__()
self.points = []
self.init_ui()
self.startTimer(0)
def init_ui(self):
self.setGeometry(300, 300, WIDTH, HEIGHT)
self.setWindowTitle('Draw')
self.show()
def timerEvent(self, e):
if len(self.points) < 10000:
self.points.append((
random.randint(1, WIDTH-1),
random.randint(1, HEIGHT-1),
))
self.update()
def paintEvent(self, e):
qp = QtGui.QPainter()
qp.begin(self)
color = QtGui.QColor(250, 100, 100)
qp.setPen(color)
for point in self.points:
qp.drawEllipse(point[0]-3, point[1]-3, 6, 6)
qp.end()
def main():
app = QtGui.QApplication(sys.argv)
ex = DrawExample()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I will assume that qp.begin(self) and qp.end() should be placed in a loop
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question