코드 기록
[Pyqt5] QMainWindow 배경 이미지 설정하기
Bono-Dev
2024. 4. 14. 22:45
위 이미지 처럼 윈도우의 배경 이미지를 설정하려면 다음과 같은 방법을 시도할 수 있습니다.
self.window.setStyleSheet("background-image: url(FILENAME); background-attachment: fixed")
하지만 이 방법은 이미지의 사이즈를 설정할 수 없습니다. 그래서 화면에 맞지 않게 이미지가 보여지죠.
그래서 QPixmap을 통해 이미지의 사이즈를 조정한 다음 QPalette 와 QBrush 를 사용해서 배경화면을 그려줘야 합니다.
def initUI(self, titleFontSize, subFontSize, font_, title, content):
self.setWindowTitle('Full Screen Presentation')
# QLabel을 사용하여 전체 화면에 텍스트를 표시
current_dir = os.path.dirname(os.path.abspath(__file__))
# 이미지 파일의 상대 경로
image_relative_path = 'public/bg.jpg'
image_absolute_path = os.path.join(current_dir, image_relative_path).replace("\\", "/")
#윈도우의 사이즈 만큼 키우기 위해
app = QApplication.instance()
screen = app.primaryScreen()
geometry = screen.availableGeometry()
# QPixmap 객체 생성
pixmap = QtGui.QPixmap(image_absolute_path)
scaled_pixmap = pixmap.scaled(geometry.width(), geometry.height()+100)
qp = QPalette()
qp.setBrush(QPalette.Background, QBrush(scaled_pixmap))
self.setPalette(qp)
---이하생략 ---
728x90
반응형