含有章节索引的中文 文章模板
::-- zhuyj [2008-12-22 01:39:36]
Contents
1. Drawing in PyQt4
PyQt4的绘图
Drawing is used, when we want to change or enhance an existing widget. Or if we are creating a custom widget from scratch. To do the drawing, we use the drawing API provided by the PyQt4 toolkit.
当我们想要改变或者加强已经存在的组件时,或者我们通过涂画自定义组件时,需要用的绘图。为了进行绘图,我们要使用PyQt4工具集提供的绘图API。
The drawing is done within the paintEvent() method. The drawing code is placed between the begin() and end() methods of the QPainter object.
- 绘图通过paintEvent()方法实现。绘图的代码放置在QPainter对象的begin() 和end()方法之间。
1.1. Drawing text
绘制文本
We begin with drawing some unicode text onto the window client area.
- 我们首先将一些Unicode文本绘制在窗口的客户区域。
1 #!/usr/bin/python
2
3 # drawtext.py
4
5 import sys
6 from PyQt4 import QtGui, QtCore
7
8
9 class DrawText(QtGui.QWidget):
10 def __init__(self, parent=None):
11 QtGui.QWidget.__init__(self, parent)
12
13 self.setGeometry(300, 300, 250, 150)
14 self.setWindowTitle('Draw Text')
15
16 self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'
17
18
19
20 def paintEvent(self, event):
21 paint = QtGui.QPainter()
22 paint.begin(self)
23 paint.setPen(QtGui.QColor(168, 34, 3))
24 paint.setFont(QtGui.QFont('Decorative', 10))
25 paint.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)
26 paint.end()
27
28
29 app = QtGui.QApplication(sys.argv)
30 dt = DrawText()
31 dt.show()
32 app.exec_()
In our example, we draw some text in azbuka. The text is vertically and horizontally aligned.
- 这个例子里,我们用azbuka语输出了一些文本。文本在水平和垂直方向都是居中的。
def paintEvent(self, event):
Drawing is done within a paint event
- 通过一个绘图事件实现绘图。
paint = QtGui.QPainter() paint.begin(self) ... paint.end()
The QPainter class is responsible for all the low-level painting. All the painting methods go between begin() and end() methods.
- QPainter类负责所有的底层绘图。所有在begin()和end()方法之间的绘图方法。
paint.setPen(QtGui.QColor(168, 34, 3)) paint.setFont(QtGui.QFont('Decorative', 10))
Here we define pen and font, which we use to draw the text.
- 这里我们定义用来输出文字的画笔和字体。
paint.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)
The drawText() method actually draws text on the window.
- drawText()方法实际上在窗口绘制文字。
Drawing Text Figure: Drawing Text
1.2. Drawing points
画点
A point is the most simple graphics object, that can be drawn. It is a small spot on the window.
- 点是可被绘制的最简单的图形对象。是窗口的一个小点。
1 #!/usr/bin/python
2
3 # points.py
4
5 import sys, random
6 from PyQt4 import QtGui, QtCore
7
8
9 class Points(QtGui.QWidget):
10 def __init__(self, parent=None):
11 QtGui.QWidget.__init__(self, parent)
12
13 self.setGeometry(300, 300, 250, 150)
14 self.setWindowTitle('Points')
15
16 def paintEvent(self, event):
17 paint = QtGui.QPainter()
18 paint.begin(self)
19 paint.setPen(QtCore.Qt.red)
20 size = self.size()
21 for i in range(1000):
22 x = random.randint(1, size.width()-1)
23 y = random.randint(1, size.height()-1)
24 paint.drawPoint(x, y)
25 paint.end()
26
27 app = QtGui.QApplication(sys.argv)
28 dt = Points()
29 dt.show()
30 app.exec_()
In our example, we draw randomly 1000 red points on the client area.
- 这个例子里,我们随机的在客户区域绘制1000个红点。
paint.setPen(QtCore.Qt.red)
We set the pen to red color. We use a predefined color constant.
- 我们设置画笔为红色,预定义的颜色常量。
size = self.size()
Each time we resize the window, a paint event is generated. We get the current size of the window with the size() method.
- 每次改变窗口的尺寸,就会生成一个绘图事件。我们通过size() 方法获取当前的窗口尺寸。
paint.drawPoint(x, y)
We draw the point with the drawPoint() method.
- 我们通过drawPoint()方法画点。
Points Figure: Points
1.3. Colors
颜色
A color is an object representing a combination of Red, Green, and Blue (RGB) intensity values. Valid RGB values are in the range 0 to 255. We can define a color in various ways. The most common are RGB decimal values or hexadecimal values. We can also use an RGBA value, which stands for Red, Green, Blue, Alpha. Here we add some extra information, regarding transparency. Alpha value of 255 defines full opacity, 0 is for full transparency, eg the color is invisible.
- 颜色是通过红,绿,蓝亮度值的组合所表现的对象。正确的RGB值是从0到255,我们可以通过不同的方法来定义一种颜色。最常见的是RGB的十进制或十六进制表示。我们也可以使用包含红,绿,蓝和alpha的RGBA值来表示。这里我们增加一些关于透明度的额外信息,alpha值为255表示完全不透明,0 表示完全透明,比如颜色完全不可见。
1 #!/usr/bin/python
2
3 # colors.py
4
5 import sys, random
6 from PyQt4 import QtGui, QtCore
7
8
9 class Colors(QtGui.QWidget):
10 def __init__(self, parent=None):
11 QtGui.QWidget.__init__(self, parent)
12
13 self.setGeometry(300, 300, 350, 280)
14 self.setWindowTitle('Colors')
15
16 def paintEvent(self, event):
17 paint = QtGui.QPainter()
18 paint.begin(self)
19
20 color = QtGui.QColor(0, 0, 0)
21 color.setNamedColor('#d4d4d4')
22 paint.setPen(color)
23
24 paint.setBrush(QtGui.QColor(255, 0, 0, 80))
25 paint.drawRect(10, 15, 90, 60)
26
27 paint.setBrush(QtGui.QColor(255, 0, 0, 160))
28 paint.drawRect(130, 15, 90, 60)
29
30 paint.setBrush(QtGui.QColor(255, 0, 0, 255))
31 paint.drawRect(250, 15, 90, 60)
32
33 paint.setBrush(QtGui.QColor(10, 163, 2, 55))
34 paint.drawRect(10, 105, 90, 60)
35
36 paint.setBrush(QtGui.QColor(160, 100, 0, 255))
37 paint.drawRect(130, 105, 90, 60)
38
39 paint.setBrush(QtGui.QColor(60, 100, 60, 255))
40 paint.drawRect(250, 105, 90, 60)
41
42 paint.setBrush(QtGui.QColor(50, 50, 50, 255))
43 paint.drawRect(10, 195, 90, 60)
44
45 paint.setBrush(QtGui.QColor(50, 150, 50, 255))
46 paint.drawRect(130, 195, 90, 60)
47
48 paint.setBrush(QtGui.QColor(223, 135, 19, 255))
49 paint.drawRect(250, 195, 90, 60)
50
51 paint.end()
52
53 app = QtGui.QApplication(sys.argv)
54 dt = Colors()
55 dt.show()
56 app.exec_()
In our example, we draw 9 colored rectangles. The first row shows a red color, with different alpha values.
- 这个例子里,我们画了9个有颜色的长方形。第一行显示了具有不同alpha值的红色。
color = QtGui.QColor(0, 0, 0) color.setNamedColor('#d4d4d4')
Here we define a color using hexadecimal notation.
- 这里我们通过使用十六进制表示来定义一个颜色
paint.setBrush(QtGui.QColor(255, 0, 0, 80)); paint.drawRect(10, 15, 90, 60)
Here we define a brush and draw a rectangle. A brush is an elementary graphics object, which is used to draw the background of a shape. The drawRect() method accepts four parameter. The first two are x, y values on the axis. The third and fourth parameters are width and height of the rectangle. The method draws a rectangle using current pen and current brush.
- 这里我们定义刷子并绘制一个矩形。刷子是用来绘制形状背景的基本绘图对象,drawRect()方法接受四个参数,头两个是坐标轴的x,y值。第三个和第四个参数是矩形的宽和高。这个方法使用当前画笔和当前刷子绘制一个矩形。
Colors Figure: Colors
1.4. QPen
QPen is an elementary graphics object. It is used to draw lines, curves and outlines of rectangles, ellipses, polygons or other shapes.
- Q画笔是一个用来画直线,曲线和矩形的轮廓,椭圆,多边形或其他图形的基本绘图对象。
1 #!/usr/bin/python
2
3 # penstyles.py
4
5 import sys
6 from PyQt4 import QtGui, QtCore
7
8
9 class PenStyles(QtGui.QWidget):
10 def __init__(self, parent=None):
11 QtGui.QWidget.__init__(self, parent)
12
13 self.setGeometry(300, 300, 280, 270)
14 self.setWindowTitle('penstyles')
15
16 def paintEvent(self, event):
17 paint = QtGui.QPainter()
18
19 paint.begin(self)
20
21 pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
22
23 paint.setPen(pen)
24 paint.drawLine(20, 40, 250, 40)
25
26 pen.setStyle(QtCore.Qt.DashLine)
27 paint.setPen(pen)
28 paint.drawLine(20, 80, 250, 80)
29
30 pen.setStyle(QtCore.Qt.DashDotLine)
31 paint.setPen(pen)
32 paint.drawLine(20, 120, 250, 120)
33
34 pen.setStyle(QtCore.Qt.DotLine)
35 paint.setPen(pen)
36 paint.drawLine(20, 160, 250, 160)
37
38 pen.setStyle(QtCore.Qt.DashDotDotLine)
39 paint.setPen(pen)
40 paint.drawLine(20, 200, 250, 200)
41
42 pen.setStyle(QtCore.Qt.CustomDashLine)
43 pen.setDashPattern([1, 4, 5, 4])
44 paint.setPen(pen)
45 paint.drawLine(20, 240, 250, 240)
46
47 paint.end()
48
49 app = QtGui.QApplication(sys.argv)
50 dt = PenStyles()
51 dt.show()
52 app.exec_()
In our example, we draw six lines. The lines are drawn in six different pen styles. There are five predefined pen styles. We can create also custom pen styles. The last line is drawn using custom pen style.
- 这个例子里我们绘制六条直线。这些线通过六种不同的笔类型绘制,其中有五种是预定义的笔类型,我们也可以自定义笔类型,最后的一根直线使用了自定义的笔类型。
pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
We create a QPen object. The color is black. The width is set to 2 pixels, so that we can see the differences between the pen styles. The QtCore.Qt.SolidLine is one of the predefined pen styles.
pen.setStyle(QtCore.Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) paint.setPen(pen)
Here we define a custom pen style. We set a QtCore.Qt.CustomDashLine pen style and call a setDashPattern() method. The list of numbers defines a style. There must be an even number of numbers. Odd numbers define a dash, even numbers space. The greater the number, the greater the space or the dash. Our pattern is 1px dash 4px space 5px dash 4px space etc.
这里我们定义一个自定义画笔类型。我们设置QtCore.Qt.CustomDashLine画笔类型并且调用setDashPattern()方法。数字列表定义一个类型,必须有偶数个数字,奇数位置定义一个破折号,偶数位置定义一个空白,数字越大,空白或者破折号就越长。我们的例子是1像素的破折,4像素的空白,5像素的破折,4像素的空白。
Pen Styles Figure: Pen Styles
1.5. QBrush
QBrush is an elementary graphics object. It is used to paint the background of graphics shapes, such as rectangles, ellipses or polygons. A brush can be of three different types. A predefined brush a gradien or a texture pattern.
- Q刷子是用来绘制图形(例如矩形,椭圆形或多边形)背景的基本图形对象。刷子可以有三种不同的类型,预定义的,gradien或者纹理样式的。
1 #!/usr/bin/python
2
3 # brushes.py
4
5 import sys
6 from PyQt4 import QtGui, QtCore
7
8
9 class Brushes(QtGui.QWidget):
10 def __init__(self, parent=None):
11 QtGui.QWidget.__init__(self, parent)
12
13 self.setGeometry(300, 300, 355, 280)
14 self.setWindowTitle('Brushes')
15
16 def paintEvent(self, event):
17 paint = QtGui.QPainter()
18
19 paint.begin(self)
20
21 brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
22 paint.setBrush(brush)
23 paint.drawRect(10, 15, 90, 60)
24
25 brush.setStyle(QtCore.Qt.Dense1Pattern)
26 paint.setBrush(brush)
27 paint.drawRect(130, 15, 90, 60)
28
29 brush.setStyle(QtCore.Qt.Dense2Pattern)
30 paint.setBrush(brush)
31 paint.drawRect(250, 15, 90, 60)
32
33 brush.setStyle(QtCore.Qt.Dense3Pattern)
34 paint.setBrush(brush)
35 paint.drawRect(10, 105, 90, 60)
36
37 brush.setStyle(QtCore.Qt.DiagCrossPattern)
38 paint.setBrush(brush)
39 paint.drawRect(10, 105, 90, 60)
40
41 brush.setStyle(QtCore.Qt.Dense5Pattern)
42 paint.setBrush(brush)
43 paint.drawRect(130, 105, 90, 60)
44
45 brush.setStyle(QtCore.Qt.Dense6Pattern)
46 paint.setBrush(brush)
47 paint.drawRect(250, 105, 90, 60)
48
49 brush.setStyle(QtCore.Qt.Dense7Pattern)
50 paint.setBrush(brush)
51 paint.drawRect(250, 105, 90, 60)
52
53 brush.setStyle(QtCore.Qt.HorPattern)
54 paint.setBrush(brush)
55 paint.drawRect(10, 195, 90, 60)
56
57 brush.setStyle(QtCore.Qt.VerPattern)
58 paint.setBrush(brush)
59 paint.drawRect(130, 195, 90, 60)
60
61 brush.setStyle(QtCore.Qt.BDiagPattern)
62 paint.setBrush(brush)
63 paint.drawRect(250, 195, 90, 60)
64
65 paint.end()
66
67 app = QtGui.QApplication(sys.argv)
68 dt = Brushes()
69 dt.show()
70 app.exec_()
In our example, we draw six different rectangles.
- 这个例子里,我们绘制六个不同的矩形。
brush = QtGui.QBrush(QtCore.Qt.SolidPattern) paint.setBrush(brush) paint.drawRect(10, 15, 90, 60)
We define a brush object. Set it to the painter object. And draw the rectangle calling the drawRect() method.
- 我们定义一个刷子对象,将它设置到绘图对象,然后通过调用drawRect()绘制矩形。
Brushes Figure: Brushes