HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📟
PyQt 5 Lecture
/
🌲
003 QFontDialog, QColorDialog (글꼴, 컬러 설정 다이얼로그)
🌲

003 QFontDialog, QColorDialog (글꼴, 컬러 설정 다이얼로그)

1. 코드2. 상세 내용3. 실행 화면

1. 코드

from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QPushButton,\ QLabel, QFontDialog, QColorDialog, QFrame from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor import sys class 글꼴컬러설정다이얼로그(QWidget): def __init__(self): super().__init__() self.UI초기화() def UI초기화(self): self.label = QLabel('안녕하세요! 제주코딩베이스 캠프 PyQt5 강좌 입니다!', self) self.label.setAlignment(Qt.AlignCenter) btn1 = QPushButton('폰트 선택', self) btn1.clicked.connect(self.showFont) color = QColor(Qt.black) self.colorFrame = QFrame(self) self.colorFrame.setStyleSheet( f'background-color: {color.name()};') btn2 = QPushButton('색상 선택', self) btn2.clicked.connect(self.showColor) vbox = QVBoxLayout() vbox.addWidget(self.label) vbox.addWidget(btn1) vbox.addWidget(self.colorFrame) vbox.addWidget(btn2) self.setLayout(vbox) self.setWindowTitle('QFont, QColor Dialog') self.setGeometry(300, 300, 450, 300) self.show() def showFont(self): font, flag = QFontDialog.getFont() if flag: self.label.setFont(font) else: self.label.setText("선택 되지 않았어요! 다시 한번 선택해주세요!") def showColor(self): color = QColorDialog.getColor() if color.isValid(): self.colorFrame.setStyleSheet(f'background-color: {color.name()}') else: self.colorFrame.setStyleSheet("image : url(img/weniv-licat.png)") 프로그램무한반복 = QApplication(sys.argv) 실행인스턴스 = 글꼴컬러설정다이얼로그() 프로그램무한반복.exec_()

2. 상세 내용

HTML로 매핑을 하자면, form에 input을 만드는 것이라고 생각하시면 됩니다. 여기서는 font와 color를 선택할 수 있도록 해보겠습니다.
  • QColorDialog 공식 문서
    • QColorDialog - Qt for Python
      The color dialog's function is to allow users to choose colors. For example, you might use this in a drawing program to allow the user to set the brush color. The static functions provide modal color dialogs. The static function shows the dialog, and allows the user to specify a color.
      QColorDialog - Qt for Python
      https://doc.qt.io/qtforpython/PySide2/QtWidgets/QColorDialog.html
      QColorDialog - Qt for Python
  • QFontDialog
    • QFontDialog - Qt for Python
      A font dialog is created through one of the static getFont() functions. Examples: The dialog can also be used to set a widget's font directly: If the user clicks OK the font they chose will be used for myWidget, and if they click Cancel the original font is used.
      QFontDialog - Qt for Python
      https://doc.qt.io/qtforpython/PySide2/QtWidgets/QFontDialog.html
      QFontDialog - Qt for Python
 
btn1 = QPushButton('폰트 선택', self) btn1.clicked.connect(self.showFont) color = QColor(Qt.black) self.colorFrame = QFrame(self) self.colorFrame.setStyleSheet(f'background-color: {color.name()};') btn2 = QPushButton('색상 선택', self) btn2.clicked.connect(self.showColor)
  • QColor(Qt.색상) 또는 QColor(r,g,b)으로도 사용가능
  • 3.6 미만 이라면 f''string 용법을 사용하지 못하므로, 아래와 같이 입력해주세요. self.colorFrame.setStyleSheet('background-color: %s' % color.name())
 
def showFont(self): font, flag = QFontDialog.getFont() if flag: self.label.setFont(font) else: self.label.setText("선택 되지 않았어요! 다시 한번 선택해주세요!")
  • getFont()로 폰트값과 bool값을 가져옵니다.
  • 만약 글꼴을 선택하고 ok버튼을 클릭하면 해당 값으로 글꼴을 변경하고 아니라면 라벨에 선택되지 않았다는 문구를 업데이트
 
def showColor(self): color = QColorDialog.getColor() if color.isValid(): self.colorFrame.setStyleSheet(f'background-color: {color.name()}') else: self.colorFrame.setStyleSheet("image : url(img/weniv-licat.png)")
  • getColor() 로 현재 선택된 값을 가져옵니다. 단,bool값은 가져오지 못함
  • 대신 isValid로 유효한지 즉, 컬러가 잘 선택되었는지 확인해 컬러값으로 Frame을 변경, 아니라면 라이캣이미지로 변경

3. 실행 화면

 
notion image
 
notion image
notion image
 
 
notion image
notion image