Life Seed

First QtWidgets Application 본문

Python/GUI - Qt for Python(Pyside6)

First QtWidgets Application

lifeseed 2025. 6. 8. 23:03

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

 

 

 

https://doc.qt.io/qtforpython-6/index.html 사이트의 Tutorial에 대한 내용을 정리한다.

 

Tutorials 를 클릭하여 확인할 수 있다.

 

 

출처 :: Qt for Python Tutorials 

https://doc.qt.io/qtforpython-6/tutorials/index.html

 

Tutorials - Qt for Python

Basic ui files Using .ui files from Designer or QtCreator with QUiLoader and pyside6-uic

doc.qt.io

 

오늘의 첫번째 예제는 QtWidgets 중 QLabel에 대한 사용예제이다.

너무 심플한 예제라 설명할 것은 따로 없을 듯하다.

일단 코드를 살펴보자.

import sys
from PySide6.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)

# Create a simple label widget
label = QLabel("Hello World!")
label.show()
app.exec()

 

 

PySide6을 사용하는 위젯 애플리케이션의 경우 항상 PySide6에서 적절한 클래스를 가져오는 것으로 시작해야 합니다.

QtWidgets 모듈 가져오기 후 QApplication 인스턴스를 생성합니다.

Qt는 명령줄에서 인수를 받을 수 있으므로 QApplication 객체에 인수를 전달할 수 있습니다.

app = QApplication(sys.argv)

 

일반적으로 인수를 전달할 필요가 없으므로 그대로 두거나 다음 접근 방식을 사용할 수 있습니다.

app = QApplication([])

 

응용 프로그램 개체를 생성한 후 QLabel 개체를 생성했습니다. 

마지막으로 app.exec() 호출하여 Qt 메인 루프에 들어가고 Qt 코드 실행을 시작합니다.

 

 

QLabel은 텍스트와 이미지를 표시할 수 있는 위젯입니다.

Text를 html 스타일로 꾸미일 수 있는 예제

import sys
from PySide6.QtWidgets import QApplication, QLabel

app = QApplication([])  # This works too, but sys.argv is more common

# This HTML approach will be valid too!
label = QLabel("<font color=red size=40>Hello World!</font>")

label.show()
app.exec()

 

 

 

반응형