Using ERIC6 and PYQT5 to achieve Python's extreme GUI Programming (series)----Drawing (Drawing) (1)--Write text

Source: Internet
Author: User
Tags drawtext

Overview

This article implements the following program: ( drawing text in a form )

The main steps are as follows:

1. Create a new project in Eric6, create a new form

2, (automatically open) into PyQt5 Desinger, edit the GUI, save

3, back to Eric 6, to the previous step to get the interface file Drawing.ui file right-click, compile the form, get ui_drawing.py file

4, then right-click on the Drawing.ui file, generate dialog code, get drawing.py file. ( add your own program logic in drawing.py )

5, Py2exe package into EXE file (this step)

Body

Next to "Overview" Step 4th:

In drawing.py, prepare the text you want to paint (self.text):

    def __init__ (Self, parent=None):          #  ...        ' \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'

Then define a painting event:

    def paintevent (Self, event):         ...

All the paintings take place within this painting event. Add four lines of code to this painting event!

        QP = qtgui.qpainter ()        qp.begin (self)        self.drawtext (event, QP) #自定义的绘画方法        qp.end ()

QtGui.QPainterClass is responsible for all low-level paintings.

All the painting methods to be put in begin() andend() 方法之间。当然,这里我们放的是自定义的drawText()方法

The above code together is this:

    def paintevent (Self, event):         = Qtgui.qpainter ()        qp.begin (self)        self.drawtext (event, QP)#自定义的绘画方法        qp.end () 

in the next few articles on painting, this piece of code is almost unchanged .

Of course, the change is the custom DrawText method:

    def DrawText (Self, event, QP):        qp.setpen (Qtgui.qcolor (168, 3)) #设置笔的颜色        Qp.setfont (Qtgui.qfont (  'decorative')) #设置字体        qp.drawtext (Event.rect (), QtCore.Qt.AlignCenter , Self.text) #画出文本

The final drawing.py is this:

#-*-coding:utf-8-*-"""Module implementing Drawing."""ImportSYS fromPyQt5ImportQtgui, Qtcore fromPyqt5.qtwidgetsImportQdialog, Qapplication fromUi_drawingImportui_drawingclassDrawing (Qdialog, ui_drawing):def __init__(Self, parent=None): Super (Drawing, self).__init__(parent) self.setupui (self) self.text='\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'    defpaintevent (Self, event): 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', 20) ) Qp.drawtext (Event.rect (), QtCore.Qt.AlignCenter, Self.text)if __name__=='__main__': App=qapplication (SYS.ARGV) dlg=Drawing () dlg.show () Sys.exit (App.exec_ ())

Eric6 and PyQt5 for Python's Extreme GUI Programming (series)----Drawing (Drawing) (1)--Write text

Related Article

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.