Python basic
  • Python 기초 수업
  • Python 설치
  • Python의 기본
    • Python 시작하기
    • 변수(Variable)
    • 산술 연산자
    • 문자열(String) 출력
    • 문장(Statement)
  • 자료형
    • 정수형(Integer)
    • 실수형(Float)
    • 부울형(Bool)
    • 복소수형(Complex)
    • 문자열형(String)
  • 조건문
  • 자료구조
    • List
    • Set
    • 튜플(Tuple)
    • 딕셔너리(Dictionary)
  • 반복문
  • 문제 타임
  • 함수(Function)
    • 기본형 함수
    • 매개변수(Parameter)와 반환값(Return Value)
    • 가변매개변수 함수
    • 함수 설명 표시
    • 변수의 범위
  • 모듈(Module)
    • 모듈의 이용
    • __name__ 그리고 '__main__'
  • 예외처리(Exception Handling)
    • try, except, else, finally
  • 클래스(Class)
    • Class 심화
  • PyQt
    • PyQt 설치
    • PyQt Widget
      • QWidget
      • QLabel
      • QLineEdit
      • QTextEdit
      • QPushButton
      • QCheckBox
      • Application Make
        • Widget의 배치 #1
        • Widget의 배치 #2
        • 숫자 맞추기 게임
        • 야구게임
  • OPEN API(공공데이터 포탈)
    • 공공데이터 사용하기
  • 크롤링 (Crawling)
    • Crawling Library 설치
    • 날씨, 미세먼지 농도
  • SQLlite
    • DB의 작성
  • Dic 참고
  • 함수 참고 코드
  • Widget 배치 시작 코드
  • 야구게임
Powered by GitBook
On this page
  • 글자
  • 글자 크기 변경
  • 글자 색깔 변경
  • 이미지
  • 이미지 비율맞추며 줄이
  1. PyQt
  2. PyQt Widget

QLabel

글자 or 이미지 표현에 사용합니다.

글자

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
class App(QLabel):
    def __init__(self):
        super(App, self).__init__()
        self.setWindowTitle('Window!!!')
        self.resize(300,400)
        self.setText('레이블 입니다.')
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec_())

07 QLabel을 상속받은 레이블을 만듭니다.

글자 크기 변경

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
class App(QLabel):
    def __init__(self):
        super(App, self).__init__()
        self.setWindowTitle('Window!!!')
        self.resize(300,400)
        self.changeFont()
        self.setText('레이블 입니다.')
    def changeFont(self):
        font = QtGui.QFont()
        font.setBold(True)
        font.setPointSize(45)
        font.setUnderline(True)
        self.setFont(font)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec_())

14 폰트를 관리하는 QFont를 생성합니다. 15 볼드 설정 옵션 16 글자 크기 조절 옵션 17 밑 줄 옵션 18 변경한 폰트 적용합니다.

글자 색깔 변경

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
from PyQt5.QtGui import QPalette
class App(QLabel):
    def __init__(self):
        super(App, self).__init__()
        self.setWindowTitle('Window!!!')
        self.resize(300,400)
        self.changeFont()
        self.changeFontColor()
        self.setText('레이블 입니다.')
    def changeFont(self):
        font = QtGui.QFont()
        font.setBold(True)
        font.setPointSize(45)
        font.setUnderline(True)
        self.setFont(font)
    def changeFontColor(self):
        pal = QtGui.QPalette()
        pal.setColor(QPalette.WindowText, QtGui.QColor(Qt.red))
        self.setPalette(pal)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec_())

22 색을 관리하는 QPalette를 가져옵니다. 23 컬러 변경에 글자와, 변경할 색상을 넣습니다. 24 변경된 값을 적용합니다.

이미지

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QIcon, QPixmap
class App(QLabel):
    def __init__(self):
        super(App, self).__init__()
        self.setWindowTitle('Window!!!')
        self.resize(300,400)
        self.setIamge()
    def setIamge(self):
        pixmap = QPixmap('/Users/evilstorm/workspace/python/basic study/pyqt/images/park_2.png')
        self.setPixmap(pixmap)
        self.resize(pixmap.width(),pixmap.height())
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec_())

12 이미지 처리하는 QPixmap을 생성합니다. 이때 이미지 경로는 Full Path를 넣어주셔야 합니다. 13 이미지를 label에 추가합니다. 14 윈도우 크기를 이미지 크기와 같게 변경합니다.

이미지 비율맞추며 줄이

준비된 윈도우 보다 이미지가 크다면 이미지가 잘리겠죠. 이미지가 잘리지 않고 윈도우 크기안에 넣는 방법입니다.

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
class App(QLabel):
    def __init__(self):
        super(App, self).__init__()
        self.setWindowTitle('Window!!!')
        self.resize(300,200)
        self.setIamge()
    def setIamge(self):
        pixmap = QPixmap('/Users/evilstorm/workspace/python/basic study/pyqt/images/park_1.jpg')
        self.setPixmap(pixmap)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec_())

10 윈도우 크기를 300, 200으로 설정했습니다.

윈도우 크기를 이미지 크기와 맞게 resize를 하지 않았습니다. 이미지가 300, 200보다 크다면 이미지가 잘려서 보일 것입니다.

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
class App(QLabel):
    def __init__(self):
        super(App, self).__init__()
        self.setWindowTitle('Window!!!')
        self.resize(300,200)
        self.setIamge()
    def setIamge(self):
        pixmap = QPixmap('/Users/evilstorm/workspace/python/basic study/pyqt/images/park_1.jpg')
        scaleChangedImage = pixmap.scaled(300, 200, Qt.KeepAspectRatio)
        self.setPixmap(scaleChangedImage)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec_())

14 Qpixmap 의 sacled 함수를 이용해 이미지가 윈도우 크기에 벗어나지 않도록 크기 변경을 합니다. Qt.KeepAspectRatio : 비율 유지, Qt.IgnoreAspectRatio : 꽉 채우기, Qt.KeepAspectRatioByExpanding : 이미지가 덜짤리는 쪽으로 비 맞추기.

PreviousQWidgetNextQLineEdit

Last updated 6 years ago