- PyQt5 Tutorial - 파이썬으로 만드는 나만의 GUI 프로그램
- 1. PyQt5 소개 (Introduction)
- 2. PyQt5 설치 (Installation)
- 3. PyQt5 기초 (Basics)
- 4. PyQt5 레이아웃 (Layout)
- 5. PyQt5 위젯 (Widget)
- QPushButton
- QLabel
- QCheckBox
- QRadioButton
- QComboBox
- QLineEdit
- QLineEdit (Advanced)
- QProgressBar
- QSlider & QDial
- QSplitter
- QGroupBox
- QTabWidget
- QTabWidget (Advanced)
- QPixmap
- QCalendarWidget
- QSpinBox
- QDoubleSpinBox
- QDateEdit
- QTimeEdit
- QDateTimeEdit
- QTextBrowser
- QTextBrowser (Advanced)
- QTextEdit
- QTableWidget
- QTableWidget (Advanced)
- 6. PyQt5 다이얼로그 (Dialog)
- 7. PyQt5 시그널과 슬롯 (Signal&Slot)
- 8. PyQt5 그림 그리기 (Updated)
- 9. PyQt5 실행파일 만들기 (PyInstaller)
- 10. PyQt5 프로그램 예제 (Updated)
- ▷ PDF ebook
- Python Tutorial
- NumPy Tutorial
- Matplotlib Tutorial
- PyQt5 Tutorial
- BeautifulSoup Tutorial
- xlrd/xlwt Tutorial
- Pillow Tutorial
- Googletrans Tutorial
- PyWin32 Tutorial
- PyAutoGUI Tutorial
- Pyperclip Tutorial
- TensorFlow Tutorial
- Tips and Examples
둥근 직사각형 그리기 (drawRoundedRect)¶
drawRoundedRect()는 모서리가 둥근 직사각형을 그릴 때 사용합니다.
drawRoundedRect()에 x, y, width, height, x-radius, y-radius의 순서로 숫자를 입력합니다.
아래 그림을 참고하세요.
예제¶
## Ex 8-4. 둥근 직사각형 그리기 (drawRoundedRect).
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QPen, QColor, QBrush
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('drawRoundedRect')
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.draw_roundedrect(qp)
qp.end()
def draw_roundedrect(self, qp):
qp.setPen(QPen(Qt.black, 3))
qp.drawRoundedRect(20, 20, 100, 100, 0, 0)
qp.drawText(40, 140, 'radius: 0')
qp.drawRoundedRect(150, 20, 100, 100, 10, 10)
qp.drawText(170, 140, 'radius: 10')
qp.drawRoundedRect(280, 20, 100, 100, 20, 20)
qp.drawText(300, 140, 'radius: 20')
qp.drawRoundedRect(20, 160, 100, 100, 30, 30)
qp.drawText(40, 280, 'radius: 30')
qp.drawRoundedRect(150, 160, 100, 100, 40, 40)
qp.drawText(170, 280, 'radius: 40')
qp.drawRoundedRect(280, 160, 100, 100, 50, 50)
qp.drawText(300, 280, 'radius: 50')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
각기 다른 둥글기를 갖는 둥근 직사각형을 그렸습니다.
x-radius, y-radius가 0, 0인 경우 직사각형이 되고, 너비, 높이의 절반인 경우 원 (또는 타원)이 됩니다.
결과는 아래와 같습니다.
def draw_roundedrect(self, qp):
brush = QBrush(Qt.SolidPattern)
qp.setBrush(brush)
qp.drawRoundedRect(20, 10, 100, 60, 15, 15)
qp.drawText(20, 90, 'Qt.SolidPattern')
brush = QBrush(Qt.Dense1Pattern)
qp.setBrush(brush)
qp.drawRoundedRect(150, 10, 100, 60, 15, 15)
qp.drawText(150, 90, 'Qt.Dense1Pattern')
brush = QBrush(Qt.Dense2Pattern)
qp.setBrush(brush)
qp.drawRoundedRect(280, 10, 100, 60, 15, 15)
qp.drawText(280, 90, 'Qt.Dense2Pattern')
brush = QBrush(Qt.HorPattern)
qp.setBrush(brush)
qp.drawRoundedRect(20, 110, 100, 60, 15, 15)
qp.drawText(20, 190, 'Qt.HorPattern')
brush = QBrush(Qt.VerPattern)
qp.setBrush(brush)
qp.drawRoundedRect(150, 110, 100, 60, 15, 15)
qp.drawText(150, 190, 'Qt.VerPattern')
brush = QBrush(Qt.CrossPattern)
qp.setBrush(brush)
qp.drawRoundedRect(280, 110, 100, 60, 15, 15)
qp.drawText(280, 190, 'Qt.CrossPattern')
brush = QBrush(Qt.BDiagPattern)
qp.setBrush(brush)
qp.drawRoundedRect(20, 210, 100, 60, 15, 15)
qp.drawText(20, 290, 'Qt.BDiagPattern')
brush = QBrush(Qt.FDiagPattern)
qp.setBrush(brush)
qp.drawRoundedRect(150, 210, 100, 60, 15, 15)
qp.drawText(150, 290, 'Qt.FDiagPattern')
brush = QBrush(Qt.DiagCrossPattern)
qp.setBrush(brush)
qp.drawRoundedRect(280, 210, 100, 60, 15, 15)
qp.drawText(280, 290, 'Qt.DiagCrossPattern')
drawRect() 예제에서와 마찬가지로 QBrush()와 setBrush()를 이용해서 둥근 직사각형의 채우기 패턴을 설정할 수 있습니다.
결과는 아래와 같습니다.
이전글/다음글
이전글 : 직사각형 그리기 (drawRect)
다음글 : 다각형 그리기 (drawPolygon)