Drawing a polygon in PyQt

后端 未结 1 1373
广开言路
广开言路 2021-01-06 08:24

Background

I would like to draw a simple shape on the screen, and I have selected PyQt as the package to use, as it seems to be the most established

相关标签:
1条回答
  • 2021-01-06 08:51

    i am not sure, what you mean with

    on the screen

    you can use QPainter, to paint a lot of shapes on any subclass of QPaintDevice e.g. QWidget and all subclasses.

    the minimum is to set a pen for lines and text and a brush for fills. Then create a polygon, set all points of polygon and paint in the paintEvent():

    import sys, math
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class MyWidget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            QtWidgets.QWidget.__init__(self, parent)
            self.pen = QtGui.QPen(QtGui.QColor(0,0,0))                      # set lineColor
            self.pen.setWidth(3)                                            # set lineWidth
            self.brush = QtGui.QBrush(QtGui.QColor(255,255,255,255))        # set fillColor  
            self.polygon = self.createPoly(8,150,0)                         # polygon with n points, radius, angle of the first point
    
        def createPoly(self, n, r, s):
            polygon = QtGui.QPolygonF() 
            w = 360/n                                                       # angle per step
            for i in range(n):                                              # add the points of polygon
                t = w*i + s
                x = r*math.cos(math.radians(t))
                y = r*math.sin(math.radians(t))
                polygon.append(QtCore.QPointF(self.width()/2 +x, self.height()/2 + y))  
    
            return polygon
    
        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.setPen(self.pen)
            painter.setBrush(self.brush)  
            painter.drawPolygon(self.polygon)
    
    app = QtWidgets.QApplication(sys.argv) 
    
    widget = MyWidget()
    widget.show()
    
    sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题