ZetCode PyQt4 tutorial basic painting

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we draw text in Russian azbuka.

author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        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'

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Draw text')
        self.show()

    # Drawing is done within the paint event.
    def paintEvent(self, event):

        # The QtGui.QPainter class is responsible for all the low-level painting. All the painting methods go between begin() and end() methods. The actual painting is delegated to the drawText() method.
        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawText(event, qp)
        qp.end()

    def drawText(self, event, qp):

        qp.setPen(QtGui.QColor(168, 34, 3))
        qp.setFont(QtGui.QFont('Decorative', 10))
        # The drawText() method draws text on the window. The rect() method of the paint event returns the rectangle that needs to be updated.
        qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)        

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

--------------------------------------------------------------------------------

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In the example, we draw randomly 1000 red points
on the window.

author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""

import sys, random
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Points')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawPoints(qp)
        qp.end()

    def drawPoints(self, qp):

        # We set the pen to red colour. We use a predefined QtCore.Qt.red colour constant.
        qp.setPen(QtCore.Qt.red)
        # Each time we resize the window, a paint event is generated. We get the current size of the window with the size() method. We use the size of the window to distribute the points all over the client area of the window.
        size = self.size()

        for i in range(1000):
            x = random.randint(1, size.width()-1)
            y = random.randint(1, size.height()-1)
            # We draw the point with the drawPoint() method.
            qp.drawPoint(x, y)     

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

--------------------------------------------------------------------------------

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

This example draws three rectangles in three
different colours. 

author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.setGeometry(300, 300, 350, 100)
        self.setWindowTitle('Colours')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawRectangles(qp)
        qp.end()

    def drawRectangles(self, qp):

        # Here we define a colour using a hexadecimal notation.
        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor('#d4d4d4')
        qp.setPen(color)

        # 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 parameters. The first two are x and y values on the axis. The third and fourth parameters are the width and height of the rectangle. The method draws the rectangle using the current pen and brush.
        qp.setBrush(QtGui.QColor(200, 0, 0))
        qp.drawRect(10, 15, 90, 60)

        qp.setBrush(QtGui.QColor(255, 80, 0, 160))
        qp.drawRect(130, 15, 90, 60)

        qp.setBrush(QtGui.QColor(25, 0, 90, 200))
        qp.drawRect(250, 15, 90, 60)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

--------------------------------------------------------------------------------

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example we draw 6 lines using
different pen styles. 

author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.setGeometry(300, 300, 280, 270)
        self.setWindowTitle('Pen styles')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawLines(qp)
        qp.end()

    def drawLines(self, qp):

        # We create a QtGui.QPen object. The colour 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 = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)

        qp.setPen(pen)
        qp.drawLine(20, 40, 250, 40)

        # Here we define a custom pen style. We set a QtCore.Qt.CustomDashLine pen style and call the 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.
        pen.setStyle(QtCore.Qt.DashLine)
        qp.setPen(pen)
        qp.drawLine(20, 80, 250, 80)

        pen.setStyle(QtCore.Qt.DashDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 120, 250, 120)

        pen.setStyle(QtCore.Qt.DotLine)
        qp.setPen(pen)
        qp.drawLine(20, 160, 250, 160)

        pen.setStyle(QtCore.Qt.DashDotDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 200, 250, 200)

        pen.setStyle(QtCore.Qt.CustomDashLine)
        pen.setDashPattern([1, 4, 5, 4])
        qp.setPen(pen)
        qp.drawLine(20, 240, 250, 240)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

--------------------------------------------------------------------------------

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

This example draws 9 rectangles in different
brush styles.

author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.setGeometry(300, 300, 355, 280)
        self.setWindowTitle('Brushes')
        self.show()

    def paintEvent(self, e):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawBrushes(qp)
        qp.end()

    def drawBrushes(self, qp):

        # We define a brush object. We set it to the painter object and draw the rectangle by calling the drawRect() method.
        brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 15, 90, 60)

        brush.setStyle(QtCore.Qt.Dense1Pattern)
        qp.setBrush(brush)
        qp.drawRect(130, 15, 90, 60)

        brush.setStyle(QtCore.Qt.Dense2Pattern)
        qp.setBrush(brush)
        qp.drawRect(250, 15, 90, 60)

        brush.setStyle(QtCore.Qt.Dense3Pattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 90, 60)

        brush.setStyle(QtCore.Qt.DiagCrossPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 90, 60)

        brush.setStyle(QtCore.Qt.Dense5Pattern)
        qp.setBrush(brush)
        qp.drawRect(130, 105, 90, 60)

        brush.setStyle(QtCore.Qt.Dense6Pattern)
        qp.setBrush(brush)
        qp.drawRect(250, 105, 90, 60)

        brush.setStyle(QtCore.Qt.HorPattern)
        qp.setBrush(brush)
        qp.drawRect(10, 195, 90, 60)

        brush.setStyle(QtCore.Qt.VerPattern)
        qp.setBrush(brush)
        qp.drawRect(130, 195, 90, 60)

        brush.setStyle(QtCore.Qt.BDiagPattern)
        qp.setBrush(brush)
        qp.drawRect(250, 195, 90, 60)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

 

时间: 2024-10-16 19:34:42

ZetCode PyQt4 tutorial basic painting的相关文章

ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window

!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This program creates a statusbar. author: Jan Bodnar website: zetcode.com last edited: September 2011 """ import sys from PyQt4 import QtGui class Example(QtGu

ZetCode PyQt4 tutorial layout management

!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows three labels on a window using absolute positioning. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from P

ZetCode PyQt4 tutorial signals and slots

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we connect a signal of a QtGui.QSlider to a slot of a QtGui.QLCDNumber. author: Jan Bodnar website: zetcode.com last edited: October 2011 ""&quo

ZetCode PyQt4 tutorial Dialogs

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQ

ZetCode PyQt4 tutorial widgets II

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we dispay an image on the window. author: Jan Bodnar website: zetcode.com last edited: September 2011 """ import sys from PyQt4 import QtGu

ZetCode PyQt4 tutorial custom widget

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we create a custom widget. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui, QtCore

ZetCode PyQt4 tutorial Drag and Drop

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple drag and drop example. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt4 import QtGui # In order

《Python数据挖掘:概念、方法与实践》——1.4节如何建立数据挖掘工作环境

1.4 如何建立数据挖掘工作环境 前面几节帮助我们更好地了解了将要从事的项目及原因.现在可以开始建立一个开发环境,支持所有项目工作了.由于本书的目的是介绍如何构建挖掘数据模式的软件,因此我们将用一种通用编程语言编写程序.Python编程语言具有非常强大且仍在不断成长.专门致力于数据挖掘的社区.这个社区已经贡献了一些非常方便的程序库,我们可以用来进行高效的处理,我们还可以依靠他们提供的许多数据类型,更快地工作. 在本书编著时,有两个版本的Python可供下载:现在被视为经典的Python 2(最新

《Python数据挖掘:概念、方法与实践》一1.4 如何建立数据挖掘工作环境

1.4 如何建立数据挖掘工作环境 前面几节帮助我们更好地了解了将要从事的项目及原因.现在可以开始建立一个开发环境,支持所有项目工作了.由于本书的目的是介绍如何构建挖掘数据模式的软件,因此我们将用一种通用编程语言编写程序.Python编程语言具有非常强大且仍在不断成长.专门致力于数据挖掘的社区.这个社区已经贡献了一些非常方便的程序库,我们可以用来进行高效的处理,我们还可以依靠他们提供的许多数据类型,更快地工作. 在本书编著时,有两个版本的Python可供下载:现在被视为经典的Python 2(最新