Здравствуйте. Я пытаюсь нарисовать волну с помощью QPainter(). Точки должны поочереди опускаться, а затем подниматься. Написал такой код:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, time
class Test(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.pts = [[80, 50], [180, 50], [280, 50], [430, 50], [580, 50], [680, 50], [780, 50]]
button = QPushButton(self)
button.setText('Print Hello')
exit_button = QPushButton(self)
exit_button.setText('Exit App')
exit_button.move(0, 50)
self.timer = QTimer(self)
QObject.connect(self.timer, SIGNAL('timeout()'), self.wave)
self.timer.start(5)
QObject.connect(button, SIGNAL('clicked()'), self.print_h)
QObject.connect(exit_button, SIGNAL('clicked()'), example.closeAllWindows)
def print_h(self):
print 'hello!'
def poly(self, pts):
return QPolygonF(map(lambda p: QPointF(*p), pts))
def paintEvent(self, event):
painter = QPainter(self)
pts = self.pts[:]
painter.setPen(QPen(QColor(Qt.darkGreen), 3))
painter.drawPolyline(self.poly(pts))
painter.setBrush(QBrush(QColor(255, 0, 0)))
painter.setPen(QPen(QColor(Qt.black), 1))
for x, y in pts:
painter.drawEllipse(QRectF(x - 4, y - 4, 8, 8))
# print pts
def wave(self):
while True:
y = 1
if self.pts[0][1] and self.pts[6][1] == 49:
y = y
if self.pts[0][1] and self.pts[6][1] == 551:
y = -y
self.pts[0][1] += y
self.pts[6][1] += y
self.update()
print self.pts
#QApplication.processEvents()
if __name__ == '__main__':
example = QApplication(sys.argv)
test2 = Test()
test2.resize(800, 600)
test2.show()
test2.raise_()
#test2.wave()
sys.exit(example.exec_())
Но изза него виснет программа, есть также альтернатива функции wave():
def wave(self):
for point in self.pts:
while point[1] < 600:
point[1] += 1
self.update()
QApplication.processEvents()
Но она делает только половину работы и меня смущает факт присутствия QApplication.processEvents(). Заранее спасибо.