如何使用Python在PyQt5中制作半圈

时间:2018-07-10 12:58:47

标签: python pyqt5

我知道您可以用drawEllipse画一个圆,但是我怎么做半个圆呢?

这是我当前的代码,可以正确显示圆圈,但半个圆圈什么也不显示。有什么想法吗?

$ sudo curl -L --fail https://github.com/docker/compose/releases/download/1.21.2/run.sh -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

1 个答案:

答案 0 :(得分:0)

  

QPainter.drawArc(x,y,w,h,a,alen)

绘制从(x,y)开始的矩形定义的弧,该矩形具有指定的宽度和高度以及给定的startAngle和spanAngle。

from PyQt5 import QtWidgets, QtCore, QtGui

class DrawImage(QtWidgets.QWidget):    
    def __init__(self):
        super(DrawImage, self).__init__()

        self.move(150,50)
        self.setFixedSize(100,100)   
        self.startA    = 5
        self.endA      = 30
        self.linewidth = 1

    def paintEvent(self, event):                                 #use to draw on the canvas
        paint = QtGui.QPainter()
        paint.begin(self)
        paint.setRenderHint(QtGui.QPainter.Antialiasing)
        # make a white drawing background
        paint.setBrush(QtCore.Qt.white)
        paint.drawRect(event.rect())        


        # +++
        paint.setPen(QtCore.Qt.red)                      
        paint.setBrush(QtCore.Qt.white)   
        paint.drawArc(15, 15, 70, 70, 0 * 16, -180 * 16)         # <-----------        


        paint.setPen(QtCore.Qt.black)
        paint.setBrush(QtCore.Qt.white)   
        paint.drawEllipse(QtCore.QRect(25, 25, 50, 50))           #outer circle

        paint.setPen(QtCore.Qt.yellow)
        paint.setBrush(QtCore.Qt.yellow)   
        paint.drawEllipse(QtCore.QRect(37.5, 37.5, 25, 25))       #  middle circle

        paint.setPen(QtCore.Qt.blue)  # white
        paint.setBrush(QtCore.Qt.white)   
        paint.drawEllipse(QtCore.QRect(43.75, 43.75, 12.5, 12.5)) #innermost circle

        #where I am trying to implement a half circle
        r = QtCore.QRect(12.5, 12.5, 20, 20)                       #<-- create rectangle
        size = r.size()                                            #<-- get rectangle size
        r.setSize(size*10)                                         #<-- set size
        startAngle = self.startA*16                                #<-- set start angle to draw arc
        endAngle = self.endA*16                                    #<-- set end arc angle
        paint.setPen(QtGui.QPen(QtGui.QColor('#000000'), self.linewidth))   #<-- arc color
        paint.setBrush(QtCore.Qt.yellow)   
        paint.drawArc(r, startAngle, endAngle)      

        paint.end()    

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = DrawImage()
    w.show()
    sys.exit(app.exec_())

enter image description here