我有以下代码,在圆的圆周上绘制椭圆,然后在椭圆之间绘制和弦。和弦画得很好,但事件轰炸了我,进入了一个无限循环。
def binomial(i, n):
return math.factorial(n) / float(
math.factorial(i) * math.factorial(n - i))
def bernstein(t, i, n):
return binomial(i, n) * (t ** i) * ((1 - t) ** (n - i))
def bezier(t, points):
n = len(points) - 1
x = y = 0
for i, pos in enumerate(points):
bern = bernstein(t, i, n)
x += pos[0] * bern
y += pos[1] * bern
return x, y
def bezier_curve_range(n, points):
for i in xrange(n):
t = i / float(n - 1)
yield bezier(t, points)
def getControlPt(x1, y1, x2, y2, t):
new_px = (1 - t) * x1 + t * x2
new_py = (1 - t) * y1 + t * y2
return new_px, new_py
class Temp1(QWidget):
def __init__(self, c, parent=None):
super(Temp1, self).__init__(parent)
self.text = "Hi"
self.cords = c
def paintEvent(self, e):
qp = QPainter(self)
qp.setPen(Qt.red)
bluePen = QPen(Qt.blue, 1, Qt.DashLine)
steps = 10000
for i in range(15):
for j in range(15):
if i == j:
continue
px, py = getControlPt(self.cords[i][0], self.cords[i][1], self.cords[j][0], self.cords[j][1], 0.55)
px1, py1 = getControlPt(px, py, self.cords[j][0], self.cords[j][1], 0.25)
px2, py2 = getControlPt(px1, py1, self.cords[j][0], self.cords[j][1], 0.75)
cp = (self.cords[i], (px, py) , (px1, py1), (px2, py2), self.cords[j])
oldPoint = cp[0]
qp.setPen(bluePen)
for point in bezier_curve_range(steps, cp):
qp.drawLine(oldPoint[0], oldPoint[1], point[0], point[1])
oldPoint = point
class Temp(QPushButton):
def __init__(self, x, y, w, h, theta, cords, parent=None):
super(Temp, self).__init__(parent)
self.w = w
self.h = h
self.x = x
self.y = y
self.text = "test"
self.angle = theta
self.cords = cords
def paintEvent(self, e):
qp = QPainter(self)
qp.setPen(Qt.red)
qp.drawEllipse(0, 0, self.w - 2, self.h -2)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.cords = []
self.initUI()
def initUI(self):
self.setWindowTitle('Points')
self.drawP()
def drawP(self):
theta = 2 * np.pi / 15
for i in range(15):
angle = theta * (i + 1)
dx = int(round(400 + 300 * np.cos(angle)))
dy = int(round(400 + 300 * np.sin(angle)))
self.cords.append((dx, dy))
for i in range(15):
t = Temp(self.cords[i][0], self.cords[i][1], 45, 20, np.degrees(angle), self.cords, parent=self)
t.setGeometry(self.cords[i][0], self.cords[i][1] , 45, 20)
t1 = Temp1(self.cords, parent = self)
t1.setGeometry(0, 0, 800, 600)
app = QApplication(sys.argv)
ex = Example()
ex.setGeometry(0, 0, 800, 600)
ex.show()
sys.exit(app.exec_())
问题在于Temp1类的painevent函数
如果你将print 'Temp1.paintEvent', e, i, j
放入Temp1.paintEvent
内嵌套的for循环中,你会发现它没有挂起,它只是很慢,每次你隐藏窗口,然后把它带回来,它必须再次经历整个paintEvent才能真正显示它。如果您耐心等待,该窗口最终会弹出。