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.QPainter
Class 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