본문 바로가기

프로그래밍/PyQt5 GUI

53. 호 그리기 (drawArc)

drawArc()에 x, y, width, height, start-angle, span-angle 순서로 숫자를 입력하여 호를 그립 때 사용합니다.

start-angle, span_angle에 각도를 숫자로 입력할 때 0 (0도)부터 5760 (360도)까지의 숫자를 입력합니다.

예를 들어 30도를 나타내고 싶다면 30 * 16을 입력합니다.

 

53-1 예제: ex52

import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import (QPainter, QPen)
from PyQt5.QtCore import Qt

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 400, 300)
        self.setWindowTitle('drawArc')
        self.show()

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        self.draw_arc(qp)
        qp.end()

    def draw_arc(self, qp):
        qp.setPen(QPen(Qt.black, 3))

        qp.drawArc(20, 20, 100, 100, 0 * 16, 30 * 16)
        qp.drawText(60, 100, '30°')

        qp.drawArc(150, 20, 100, 100, 0 * 16, 60 * 16)
        qp.drawText(190, 100, '60°')

        qp.drawArc(280, 20, 100, 100, 0 * 16, 90 * 16)
        qp.drawText(320, 100, '90°')

        qp.drawArc(20, 140, 100, 100, 0 * 16, 180 * 16)
        qp.drawText(60, 270, '180°')

        qp.drawArc(150, 140, 100, 100, 0 * 16, 270. * 16)
        qp.drawText(190, 270, '270°')

        qp.drawArc(280, 140, 100, 100, 0 * 16, 360 * 16)
        qp.drawText(320, 270, '360°')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MyApp()
    sys.exit(app.exec_())

각도에 따라 다양한 호가 그려집니다.

53-2 결과