Qt는 GUI 프로그램 개발에 널리 쓰이는 크로스 플랫폼 프레임워크, C++프로그래밍 언어 사용
Qt5 설치 디렉토리 : C:\Anaconda3\Lib\site-packages\PyQt5
Qt Designer 실행 파일 : C:\Anaconda3\Library\bin\designer.exe
1. GUI 프로그램 구동 방법
1) GUI 레이아웃 작성
2) 시그널-슬롯 연결 및 슬롯 처리 함수(메서드) 작성
3) 실행 및 이벤트 루프
# PyQt에서는 QApplication 객체에서 exec_ 메서드를 호출해 이벤트 루프를 생성
# 이벤트를 처리할 함수 또는 메서드를 구현 => 슬롯(slot) = 콜백 함수(callback function)
import sys
from PyQt5.QtWidgets import *
app = QApplication(sys.argv) # 자체 파일 실행
# 위젯 추가
label = QLabel("Hello PyQt")
label.show()
app.exec_() # 이벤트 루프


2. Qt Designer 사용법
Python GUI's with PyQt
This tutorial covers the basics of how to create GUI's in Python using PyQt5. I will demonstrate how to find the designer and basic usage, converting .ui to .py, running the output file and connecting events to methods.
nitratine.net
1) Designer를 이용하여 GUI 생성

미리보기 : Ctrl + R
저장 : Ctrl + S
기본 저장 위치 : C:\Anaconda3\Library\bin
파일 형식 : main_window.ui (xml 형식)

2) ui파일 -> py파일 변환
실행 파일 : C:\Anaconda3\Lib\site-packages\PyQt5\uic\pyuic.py
파워셀 이용하여 pyuic.py 파일 실행하여 변환(파워셀 현재 디렉토리에 변환 파일 저장)
ui데렉토리로 이동 하여 Shift + 오른마우스 : 파워셀을 이용하여 명령 실행
실행 명령 : python -m PyQt5.uic.pyuic -x MainWindow1.ui -o MainWindow1.py

# 생성된 py파일 구성 내역
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
......
def retranslateUi(self, MainWindow):
......
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
3) CMD에서 py파일 Test
명령어 : python MainWindow1.py

3. 파이썬에서 UI파일 다루기
1) 변환py파일을 파이썬 코드에서 로드하기
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import MainWindow1
class MyWindow(QMainWindow, MainWindow1.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb1.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self, "message", "clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
2) UI 파일을 파이썬 코드에서 로드하기
ui파일과 구동py파일을 동일 디렉토리에 저장 필요
loadUoType 이용 방법
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("MainWindow1.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb1.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self, "message", "clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
loadUi 이용
from PyQt5.QtWidgets import *
from PyQt5 import uic
import MainWindow1
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("MainWindow1.ui", self)
self.pb1.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self, "message", "clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()

[예제2]
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("MainWindow2.ui")[0]
class MyWindow(QMainWindow,form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.lb1.setText("")
self.pb1.setText('Click')
self.pb2.setText("Clear")
self.pb1.clicked.connect(self.clicked_slot1)
self.pb2.clicked.connect(self.clicked_slot2)
def clicked_slot1(self):
self.lb1.setText('Msg: 버튼이 클릭되었습니다.')
def clicked_slot2(self):
self.lb1.setText('')
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec_()

How to Import a PyQt5 .ui File in a Python GUI
In this tutorial, I explain how to import .ui files created using PyQt5's designer tool in Python and how to connect widgets from the GUI to methods in Python.
nitratine.net
예제로 배우는 파이썬 프로그래밍 - PyQt QtDesigner
1. QtDesigner 소개 지금까지 GUI 프로그램의 기본 개념들을 이해하기 위해 파이썬 코드를 사용하여 UI 요소들을 생성하고 사용하였다. 하지만, 복잡한 UI 인 경우 혹은 UI 작업이 많은 경우, 일일이 UI
pythonstudy.xyz
'PyQt5' 카테고리의 다른 글
사용자 정의 Signal (0) | 2022.04.30 |
---|---|
Qt5 예제 (0) | 2022.03.21 |
[PyQt5] Widgets (0) | 2022.02.23 |
댓글