-
PYSIDE6 레이아웃PYTHON(파이썬)/PYSIDE6(GUI) 2024. 10. 22. 06:51728x90반응형
위젯을 배치하고 크기를 조절하는 것은 GUI 애플리케이션을 개발할 때 매우 중요합니다. PySide6는 이러한 작업을 쉽게 할 수 있도록 다양한 레이아웃 클래스를 제공합니다. 레이아웃은 위젯을 창 내에서 어떻게 배치할지 정의하며, 위젯의 크기와 위치를 자동으로 조정합니다.
#### 기본 레이아웃 클래스
PySide6에서 사용할 수 있는 주요 레이아웃 클래스는 다음과 같습니다:
- QHBoxLayout: 위젯을 수평으로 배치합니다.
- QVBoxLayout: 위젯을 수직으로 배치합니다.
- QGridLayout: 위젯을 격자 형태로 배치합니다.
- QFormLayout: 양식 레이아웃으로, 레이블과 필드를 쌍으로 배치합니다.
각 레이아웃 클래스를 사용하는 방법을 살펴보겠습니다.
**QHBoxLayout**
```python
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QHBoxLayout Example")
layout = QHBoxLayout()
layout.addWidget(QPushButton("Button 1"))
layout.addWidget(QPushButton("Button 2"))
layout.addWidget(QPushButton("Button 3"))
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
```
이 예제는 세 개의 버튼을 수평으로 배치합니다.
**QVBoxLayout**
```python
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QVBoxLayout Example")
layout = QVBoxLayout()
layout.addWidget(QPushButton("Button 1"))
layout.addWidget(QPushButton("Button 2"))
layout.addWidget(QPushButton("Button 3"))
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
```
이 예제는 세 개의 버튼을 수직으로 배치합니다.
**QGridLayout**
```python
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QGridLayout Example")
layout = QGridLayout()
layout.addWidget(QPushButton("Button 1"), 0, 0)
layout.addWidget(QPushButton("Button 2"), 0, 1)
layout.addWidget(QPushButton("Button 3"), 1, 0)
layout.addWidget(QPushButton("Button 4"), 1, 1)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
```
이 예제는 네 개의 버튼을 2x2 그리드에 배치합니다.
**QFormLayout**
```python
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QFormLayout, QLineEdit, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QFormLayout Example")
layout = QFormLayout()
layout.addRow(QLabel("Name:"), QLineEdit())
layout.addRow(QLabel("Email:"), QLineEdit())
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
```
이 예제는 양식 레이아웃을 사용하여 두 개의 레이블과 텍스트 필드를 쌍으로 배치합니다.
#### 중첩 레이아웃
때로는 여러 레이아웃을 중첩하여 더 복잡한 레이아웃을 만들 필요가 있습니다. 다음 예제는 수직 레이아웃 내에 수평 레이아웃을 중첩하는 방법을 보여줍니다.
```python
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Nested Layouts Example")
main_layout = QVBoxLayout()
top_layout = QHBoxLayout()
top_layout.addWidget(QPushButton("Top Button 1"))
top_layout.addWidget(QPushButton("Top Button 2"))
bottom_layout = QHBoxLayout()
bottom_layout.addWidget(QPushButton("Bottom Button 1"))
bottom_layout.addWidget(QPushButton("Bottom Button 2"))
main_layout.addLayout(top_layout)
main_layout.addLayout(bottom_layout)
container = QWidget()
container.setLayout(main_layout)
self.setCentralWidget(container)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
```
이 예제에서는 수직 레이아웃 내에 두 개의 수평 레이아웃을 중첩하여 네 개의 버튼을 배치합니다.
#### 레이아웃 속성
각 레이아웃 클래스는 다양한 속성을 제공하여 레이아웃의 동작을 제어할 수 있습니다. 예를 들어, 여백과 간격을 설정할 수 있습니다.
**여백 설정**
```python
layout.setContentsMargins(10, 10, 10, 10)
```
여백은 위젯과 레이아웃 경계 사이의 공간을 설정합니다.
**간격 설정**
```python
layout.setSpacing(20)
```
간격은 위젯 간의 공간을 설정합니다.
이 장에서는 PySide6에서 제공하는 다양한 레이아웃을 사용하여 위젯을 배치하는 방법을 살펴보았습니다. 다음 장에서는 메뉴와 도구 모음을 사용하여 애플리케이션을 더욱 기능적으로 만드는 방법을 알아보겠습니다.
import sysfrom PySide6.QtWidgets import QApplication, QMainWindow#from PySide6.QtGui import QColorfrom layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")widget = Color("red")self.setCentralWidget(widget)app = QApplication(sys.argv)window = MainWindow()window.show()app.exec()import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QVBoxLayout,QWidget,)
from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout = QVBoxLayout()layout.addWidget(Color("red"))widget = QWidget()widget.setLayout(layout)self.setCentralWidget(widget)app = QApplication(sys.argv)window = MainWindow()window.show()app.exec()import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QVBoxLayout,QWidget,)from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout = QVBoxLayout()layout.addWidget(Color("red"))layout.addWidget(Color("grddn"))layout.addWidget(Color("blue"))widget = QWidget()widget.setLayout(layout)self.setCentralWidget(widget)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QHBoxLayout,QWidget,)from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout = QHBoxLayout()layout.addWidget(Color("red"))layout.addWidget(Color("green"))layout.addWidget(Color("blue"))widget = QWidget()widget.setLayout(layout)self.setCentralWidget(widget)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QHBoxLayout,QVBoxLayout,QWidget,)from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout1 = QHBoxLayout()layout2 = QVBoxLayout()layout3 = QVBoxLayout()layout2.addWidget(Color("red"))layout2.addWidget(Color("yellow"))layout2.addWidget(Color("purple"))layout1.addLayout(layout2)layout1.addWidget(Color("green"))layout3.addWidget(Color("red"))layout3.addWidget(Color("purple"))layout1.addLayout(layout3)widget = QWidget()widget.setLayout(layout1)self.setCentralWidget(widget)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QHBoxLayout,QVBoxLayout,QWidget,)
from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout1 = QHBoxLayout()layout2 = QVBoxLayout()layout3 = QVBoxLayout()layout1.setContentsMargins(0,0,0,0)layout1.setSpacing(20)layout2.addWidget(Color("red"))layout2.addWidget(Color("yellow"))layout2.addWidget(Color("purple"))layout1.addLayout(layout2)layout1.addWidget(Color("green"))layout3.addWidget(Color("red"))layout3.addWidget(Color("purple"))layout1.addLayout(layout3)widget = QWidget()widget.setLayout(layout1)self.setCentralWidget(widget)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QGridLayout,QWidget,)from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout = QGridLayout()layout.addWidget(Color("red"), 0, 0)layout.addWidget(Color("green"), 1, 0)layout.addWidget(Color("blue"), 1, 1)layout.addWidget(Color("purpld"), 2, 0)widget = QWidget()widget.setLayout(layout)self.setCentralWidget(widget)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sysfrom PySide6.QtWidgets import (QApplication,QMainWindow,QStackedLayout,QWidget,)from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout = QStackedLayout()layout.addWidget(Color("red"))layout.addWidget(Color("green"))layout.addWidget(Color("blue"))layout.addWidget(Color("yellow"))layout.setCurrentIndex(3)widget = QWidget()widget.setLayout(layout)self.setCentralWidget(widget)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sysfrom PySide6.QtWidgets import(QApplication,QMainWindow,QHBoxLayout,QVBoxLayout,QStackedLayout,QPushButton,QWidget,)
from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")
pagelayout = QVBoxLayout()button_layout = QHBoxLayout()self.stacklayout = QStackedLayout()pagelayout.addLayout(button_layout)pagelayout.addLayout(self.stacklayout)btn = QPushButton("red")btn.pressed.connect(self.activate_tab_1)button_layout.addWidget(btn)self.stacklayout.addWidget(Color("red"))btn = QPushButton("green")btn.pressed.connect(self.activate_tab_2)button_layout.addWidget(btn)self.stacklayout.addWidget(Color("green"))btn = QPushButton("yellow")btn.pressed.connect(self.activate_tab_3)button_layout.addWidget(btn)self.stacklayout.addWidget(Color("yellow"))widget = QWidget()widget.setLayout(pagelayout)self.setCentralWidget(widget)def activate_tab_1(self):self.stacklayout.setCurrentIndex(0)def activate_tab_2(self):self.stacklayout.setCurrentIndex(1)
def activate_tab_3(self):self.stacklayout.setCurrentIndex(2)
app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sysfrom PySide6.QtWidgets import(QApplication,QMainWindow,QTabWidget,)from layout_colorwidget import Color
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")tabs = QTabWidget()tabs.setTabPosition(QTabWidget.West)tabs.setMovable(True)for color in ["red", "green", "blue", "yellow"]:tabs.addTab(Color(color), color)self.setCentralWidget(tabs)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()import sys
from PySide6.QtWidgets import (QApplication,QMainWindow,QFormLayout,QComboBox,QLineEdit,QSpinBox,QWidget,)
class MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("My App")layout = QFormLayout()self.name = QLineEdit()self.age = QSpinBox()self.age.setRange(0, 200)self.icecream = QComboBox()self.icecream.addItems(["Vanilla", "Strawberry", "Chocolate"])layout.addRow("Name", self.name)layout.addRow("Age", self.age)layout.addRow("Favorite Ice cream", self.icecream)widget = QWidget()widget.setLayout(layout)self.setCentralWidget(widget)app = QApplication(sys.argv)
window = MainWindow()window.show()
app.exec()from PySide6.QtGui import QColor, QPalettefrom PySide6.QtWidgets import QWidget
class Color(QWidget):def __init__(self, color):super().__init__()self.setAutoFillBackground(True)palette = self.palette()palette.setColor(QPalette.Window, QColor(color))self.setPalette(palette)728x90'PYTHON(파이썬) > PYSIDE6(GUI)' 카테고리의 다른 글
PYSIDE6 대화상자 (1) 2024.10.24 PYSIDE6 액션, 도구 모음, 메뉴 (0) 2024.10.23 PYSIDE6 위젯 (0) 2024.10.21 PYSIDE6 시그널과 슬롯 (0) 2024.10.20 PYSIDE 첫번째 APP (0) 2024.10.19