Operation Manual

Experiments in Python
96
Notes:
Line drawing
# Include the system, maths and graphical user interface modules
import sys, m at h
from PySide.QtGui import *
from P ySid e.Qt Core import *
class SketchWindow( Q W i d g e t ):
def __init__(self, title, draw, size):
super(SketchWindow, self).__init__()
self.setWindowTitle(title)
width, height = size
self.setGeometry(60, 60, width, height)
self.windowSize = size
self.draw = draw
def paintEvent(self, event):
qp = QPainter()
q p.b e gin(self)
pen = QPen(Qt.yellow, 4)
qp.setPen(pen)
self.draw(qp, self.windowSize)
q p.end()
# this draw function is not within the SketchWindow class
def drawSine(context, size):
width, height = size
points = []
for x in range( w i d t h ):
angle = 5 * x * 2 * math.pi / width
qpoint = QPoint(x, (height/2) * (1+math.sin(angle)) )
points.append(qpoint)
context.drawPolyline(points)
# create the application and widget and start it
application = QApplication(sys.argv)
widget = SketchWindow('Sine', drawSine, (320,200))
wid g et.show()
application.exec_()