Post

PySide6를 공부해보자!

고양이 프로젝트를 위해!

pyqt와 pyside의 차이가 무엇인가?

Pyside6와 PyQt 모두 Qt를 활용한 툴이다.

GUI 개발에 사용되는 툴킷으로 많이 사용되고 있다. (나는 고양이 만들거지만)

PySide6와 PyQt의 차이점이라고 한다면 PySide6는 LGPL 라이선스를 가지고 있는 오픈소스라는 것이다!

하지만 PyQt는 GPL 라이선스로 제한된 버전만 이용할 수 있다. (라고 해도 사실 지금으로써는 나에게 디메리트는 없다.)

시작 (기본적인 프레임 만들기)

pip를 이용해 설치

1
pip install PySide6

제대로 깔렸는지 확인해 보기 위해 기본적인 몸통을 만들어보자

1
2
3
4
5
6
7
8
9
from PySide6.QtWidgets import QApplication, QWidget
import sys

app = QApplication(sys.argv)

window = QWidget()
window.show()

app.exec()

스크린샷 2023-12-13 123231

그렇다면 어떤 방식으로 작동을 하게 되는가?

Pythonguis에 써있는 것을 내 식대로 풀어보자면, 윈도우 스크린을 만들기 전에 시작을 하게 되면 처음 설정을 받아서 Qt 세팅을 끝낸다.

기본적인 코어는 Qt Applications의 QApplication 클래스 라고 한다. Applitcation은 딱 한번만 필요하고 그 객체는 상호작용에 따른 이벤트를 받아서 handler로 보내 작동하는 방식이라고 한다.

event-loop

출처 : python GUI’s

app.exec은 프로그램을 종료하면 그에 프로그램을 끄는 장치이다. exec은 execute의 약자이며 종료시 0을 return한다.

쉽게말해 아래와 같은 과정을 거치게 될 것이다.

app생성</br> -> app무한루프</br> -> 무한루프 탈출시 종료(sys.exit(app.exec()))

오브젝트(label) 생성하기

윈도우에 이제 처음으로 label을 만들어서 글을 출력해보자

1
2
3
4
5
6
7
8
9
import sys
from PySide6.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)

label = QLabel("HI") #라벨 만들기
label.show() #윈도우(라벨) 보이기

sys.exit(app.exec())

오브젝트(push button) 생성하기

윈도우에 클릭할 수 있는 버튼을 만들어보자

1
2
3
4
5
6
7
8
9
import sys
from PySide6.QtWidgets import QApplication, QPushButton

app = QApplication(sys.argv)

window = QPushButton("push") #버튼 생성
window.show() #윈도우(버튼) 보이기

sys.exit(app.exec())

오브젝트 배치 심화

푸시버튼을 class로 만들어서 저장, setWindowTitle을 이용해서 앱 이름을 정하고 setCentralWidget을 이용해서 중간에 배치

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import sys

from PySide6.QtCore import QSize, Qt
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("APP")

        button = QPushButton("PUSH")

        self.setCentralWidget(button)

app = QApplication(sys.argv)

window = MainWindow()
window.show()
sys.exit(app.exec())

윈도우 크기 고정

setFixedSize를 이용해 크기를 고정시켜준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys

from PySide6.QtCore import QSize, Qt
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("APP")

        button = QPushButton("PUSH")

        self.setFixedSize(QSize(400,300))#크기고정

        self.setCentralWidget(button)

app = QApplication(sys.argv)

window = MainWindow()
window.show()
sys.exit(app.exec())

버튼 클릭 감지(signal)

버튼을 눌렀을 때 그 값을 리턴받아 출력하는 방법.

setCheckable를 이용해 버튼이 체크 가능한 상태인지 정한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("APP")

        button = QPushButton("PUSH")
        button.setCheckable(True)
        button.clicked.connect(self.the_button_was_clicked)

        self.setCentralWidget(button)

    def the_button_was_clicked(self):
        print("Clicked!")

app = QApplication(sys.argv)

window = MainWindow()
window.show()

sys.exit(app.exec())

기록

2023-12-13

Creating your first app with PySide6 O

PySide6 Signals, Slots & Events X

공부자료

This post is licensed under CC BY 4.0 by the author.