Overview
This article implements the following program: ( draw a variety of different styles of lines in the 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, the paint event is defined first:
def paintevent (Self, event): = Qtgui.qpainter () qp.begin (self) # Custom Draw line method qp.end ()
Then, customize the line drawing method:
defdrawlines (Self, QP): Pen= Qtgui.qpen (QtCore.Qt.black, 2, QtCore.Qt.SolidLine) qp.setpen (pen) qp.drawline (20, 40, 250, 40) 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)
Together, drawing.py is like 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)defpaintevent (Self, event): QP=Qtgui.qpainter () qp.begin (self) self.drawlines (QP) qp.end ()defdrawlines (Self, QP): Pen= Qtgui.qpen (QtCore.Qt.black, 2, QtCore.Qt.SolidLine) qp.setpen (pen) qp.drawline (20, 40, 250, 40) 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)if __name__=='__main__': App=qapplication (SYS.ARGV) dlg=Drawing () dlg.show () Sys.exit (App.exec_ ())
Using ERIC6 and PYQT5 to achieve Python's extreme GUI Programming (series)----Drawing (Drawing) (3)--Draw line