본문 바로가기

카테고리 없음

Pyqt5 - AI 프로젝트 - 제 2장 위젯 이벤트와 레이아웃 조정, 사운드 등

여는 글

 


구현한 기능은 다음과 같습니다.

1. 버튼 위젯 (QPushButton)

2. 라벨 위젯 (QLabel)

3. 버튼 click event connect 함수로 연결하기

4. 위젯 style sheet 적용하여 Font 적용, Color 변경, Hover 효과 등 구현

5. Hover Event를 installEventFilter 함수를 사용하여 구현

6. grid layout을 사용하여 레이아웃에 위젯 배치, 각각의 차지하는 로우(행)와 컬럼(열)을 셋팅

7. setMaximumHeight() 함수를 이용하여 grid 레이아웃에서 위젯의 세로크기도 조정가능하게 만들기 (width는 기본 설정이 늘어남)

6. 배경 음악을 playsound 모듈을 이용하여 삽입

7. multiprocessing 모듈을 이용하여 sub process 를 생성하고, playsound 모듈 실행시키기

8. QAction 클래스를 이용하여 Quit(종료) 이벤트 시, sub process 종료

9. paintEvent 함수를 정의하여 어플리케이션의 background image 설정하기


오늘의 결과



오늘의 코드


import sys, os
import wx
import PyQt5
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt,QEvent

from PyQt5.QtMultimedia import QSound
from playsound import playsound
import multiprocessing
# App 관련 Class 생성
class AppForm(QtWidgets.QWidget):
# 기본 스크린 사이즈
sc_width = 1920
sc_height = 1080
m_process = multiprocessing.Process(target=playsound, args=['musics/space.mp3'])

def playBackgroundSound(self):
AppForm.m_process.start()

def stopBackgroundSound(self):
AppForm.m_process.terminate()

# 클래스 중 가장 먼저 실행되는 함수
def __init__(self):
super().__init__()
self.init_ui()

# UI 초기화 함수 (기본 UI 설정)

def init_ui(self):
#set title

self.setWindowTitle("Ai Application Demo with pyQt5")

if os.name == 'nt':
self.setWindowIcon(QIcon('app_icon.png'))

# set Defalut Screen Size
#set Defalut Screen Size - 1. get Screen Size on each platform from wxPython
wxApp = wx.App(False)
sc_width, sc_height = wx.GetDisplaySize()
# set Defalut Screen Size - 2. resize app screen size of your desktop screen size (About 1/4 size)
self.resize(int(sc_width/2) ,int(sc_height/2))

# Quit action

quit = QtWidgets.QAction("Quit", self)

quit.triggered.connect(self.close)

# set main btn
self.vision_btn = QtWidgets.QPushButton('비전', self)
self.qt_practice_btn = QtWidgets.QPushButton('QT 예제', self)

# set btn font
self.vision_btn.setFont(QFont('Arial',20))
self.qt_practice_btn.setFont(QFont('Arial',20))

self.vision_btn.setMaximumHeight(80)
self.qt_practice_btn.setMaximumHeight(80)

# set widget connect with signal
self.vision_btn.clicked.connect(self.vision_btn_function)
self.qt_practice_btn.clicked.connect(self.qt_practice_btn_function)

self.vision_btn.installEventFilter(self)
self.qt_practice_btn.installEventFilter(self)

self.vision_btn.setStyleSheet('QPushButton::hover'
'{'
'background-color : #64b5f6'
'}'
)

self.qt_practice_btn.setStyleSheet('QPushButton::hover'
'{'
'background-color : #64b5f6'
'}'
)

#set text


title_text = QtWidgets.QLabel('Vision Project with PyQt5')
title_text.setStyleSheet('color : white')
title_text.setFont(QFont('Arial',35))
title_text.setAlignment(Qt.AlignCenter)

# set grid layout
grid = QtWidgets.QGridLayout()


self.setLayout(grid)
grid.cellRect(5,5)
grid.setColumnStretch(0,1)
grid.setColumnStretch(1,1)
grid.setColumnStretch(2,1)

grid.setRowStretch(0,1)
grid.setRowStretch(1,1)
grid.setRowStretch(2,1)
grid.setRowStretch(3,1)

grid.addWidget(title_text,0,1)

grid.addWidget(self.vision_btn,1,1)
grid.addWidget(self.qt_practice_btn,2,1)

# button onclick function
def vision_btn_function(self):
print('qt_practice_btn_function page')
reply = QtWidgets.QMessageBox.question(self, 'Message', '준비중입니다 : (',
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)

def qt_practice_btn_function(self):
print('qt_practice_btn_function page')
reply = QtWidgets.QMessageBox.question(self, 'Message', '준비중입니다 : (',
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)

# set background img
def paintEvent(self, a0: QPaintEvent):
painter = QPainter(self)
painter.drawRect(self.rect())
pix = QPixmap("./imgs/ui/background-img.png") # Change to the relative path of your own image
painter.drawPixmap(self.rect(), pix)

def closeEvent(self, a0: QCloseEvent):
self.stopBackgroundSound()

def eventFilter(self, obj, event):

if obj == self.qt_practice_btn and event.type() == QEvent.HoverEnter:
self.onHovered()
elif obj == self.vision_btn and event.type() == QEvent.HoverEnter:
self.onHovered()

return super(AppForm, self).eventFilter(obj, event)

def onHovered(self):
QSound.play('musics/button_hover.wav')
# playsound('musics/button_hover.wav')



# python main code(실행 메인 스크립트)
if __name__ == '__main__':

# QApplication 함수 호출을 통해 app 생성 (모든 QT Application은 어플리케이션 객체를 생성해야한다. doc 참조.
app = QtWidgets.QApplication(sys.argv)

# AppForm 인스턴스 생성 및 창 실행

form = AppForm()
form.paintEngine()
form.show()

form.playBackgroundSound()
# App 호출, pyqt4와의 호환성을 위해 sys.exit(app.exec_())로 쓰기도

sys.exit(app.exec_())