Zetcode PyQt4 Tutorial Basic painting

Source: Internet
Author: User
Tags drawtext

#!/usr/bin/python#-*-coding:utf-8-*-"""Zetcode PYQT4 Tutorial In this example, we draw text on Russian Azbuka.author:Jan Bodnarwebsite:zetcode.com last EDI Ted:september"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (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.    defpaintevent (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 ()defDrawText (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)defMain (): 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-red points on the Window.author:Jan Bodnarwebsite:zetco De.com last Edited:september"""Importsys, Random fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): Self.setgeometry (300, 300, 280, 170) Self.setwindowtitle ('Points') self.show ()defpaintevent (Self, e): QP=Qtgui.qpainter () qp.begin (self) self.drawpoints (QP) qp.end ()defdrawpoints (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 () forIinchRange (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)defMain (): 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 threedifferent colours. Author:jan Bodnarwebsite:zetco De.com last Edited:september"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): Self.setgeometry (300, 300, 350, 100) Self.setwindowtitle ('Colours') self.show ()defpaintevent (Self, e): QP=Qtgui.qpainter () qp.begin (self) self.drawrectangles (QP) qp.end ()defdrawrectangles (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 are used to draw the background of a shape. The DrawRect () method accepts four parameters. The first is 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)                      defMain (): 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 usingdifferent pen styles. Author:jan bodnarwebsite:zetcode.c Om Last Edited:september"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): Self.setgeometry (300, 300, 280, 270) Self.setwindowtitle ('Pen Styles') self.show ()defpaintevent (Self, e): QP=Qtgui.qpainter () qp.begin (self) self.drawlines (QP) qp.end ()defdrawlines (Self, QP):#We Create a Qtgui.qpen object. The colour is black. The width is set to 2 pixels so, 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 is 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)                      defMain (): 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 Differentbrush Styles.author:Jan Bodnarwebsite:zetcode.com Last Edited:september"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): Self.setgeometry (300, 300, 355, 280) Self.setwindowtitle ('Brushes') self.show ()defpaintevent (Self, e): QP=Qtgui.qpainter () qp.begin (self) self.drawbrushes (QP) qp.end ()defdrawbrushes (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)                      defMain (): App=qtgui.qapplication (SYS.ARGV) ex=Example () sys.exit (App.exec_ ())if __name__=='__main__': Main ()

Zetcode PyQt4 Tutorial Basic painting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.