HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📟
PyQt 5 Lecture
/
🌐
003 수직, 수평 레이아웃
🌐

003 수직, 수평 레이아웃

 
1. 코드2. 상세 내용3. 실행 결과

1. 코드

import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QVBoxLayout from PyQt5.QtGui import QPixmap class 레이아웃(QWidget): def __init__(self): super().__init__() self.UI초기화() def UI초기화(self): label1_img = QLabel() label1_img.setPixmap(QPixmap('img/weniv-licat.png')) label1 = QLabel('내 이름은 라이캣!') label2_img = QLabel() label2_img.setPixmap(QPixmap('img/weniv-mura.png')) label2 = QLabel('내 이름은 무라!') vbox1 = QVBoxLayout() vbox2 = QVBoxLayout() vbox1.addWidget(label1_img) vbox1.addWidget(label1) vbox2.addWidget(label2_img) vbox2.addWidget(label2) hbox = QHBoxLayout() hbox.addLayout(vbox1) hbox.addLayout(vbox2) self.setLayout(hbox) self.setWindowTitle('Box Layout') self.setGeometry(300, 300, 400, 300) self.show() 프로그램무한반복 = QApplication(sys.argv) 실행인스턴스 = 레이아웃() 프로그램무한반복.exec_()

2. 상세 내용

여기서 주의하셔야 할 것은 addWidget과 addLayout을 구분하는 것입니다. 수평, 수직 또는 격자로 레이아웃 구성을 하신 다음, 또다른 수평, 수직 또는 격자로 구성한 레이아웃에 삽입하려 하신다면 addWidget이 아니라 addLayout을 통해 레이아웃을 추가하셔야 합니다.
  • QHBoxLayout : 수평(Horizontal)으로 쌓는 레이아웃
QHBoxLayout - Qt for Python
This class is used to construct horizontal box layout objects. See for details. The simplest use of the class is like this: First, we create the widgets we want in the layout. Then, we create the object and add the widgets into the layout. Finally, we call to install the object onto the widget.
QHBoxLayout - Qt for Python
https://doc.qt.io/qtforpython/PySide2/QtWidgets/QHBoxLayout.html
  • QVBoxLayout : 수직(Vertical)으로 쌓는 레이아웃
QVBoxLayout - Qt for Python
This class is used to construct vertical box layout objects. See for details. The simplest use of the class is like this: First, we create the widgets we want in the layout. Then, we create the object and add the widgets into the layout. Finally, we call to install the object onto the widget.
QVBoxLayout - Qt for Python
https://doc.qt.io/qtforpython/PySide2/QtWidgets/QVBoxLayout.html
vbox1 = QVBoxLayout() vbox2 = QVBoxLayout() vbox1.addWidget(label1_img) vbox1.addWidget(label1) vbox2.addWidget(label2_img) vbox2.addWidget(label2) hbox = QHBoxLayout() hbox.addLayout(vbox1) hbox.addLayout(vbox2) self.setLayout(hbox)
  • addWidget(추가할 위젯)
  • 여기서는 hbox(수평 레이아웃에) 1개의 vbox(수직 레이아웃)이 담겨진 상태
  • setLayout(연결시킬 레이아웃)

3. 실행 결과

notion image
수평 레이아웃 안에 수직 레이아웃을 2개 배치하였고, 각각에 수직 레이아웃에 이미지와 텍스트 위젯을 추가한 화면구성입니다.