1. ๋ชฉํ‘œ

๋”๋ณด๊ธฐ

 

  • QFontDialog ๋ฅผ ์‚ฌ์šฉํ•ด ํฐํŠธ๋ฅผ ์„ ํƒํ•˜๊ณ  QTextEdit ์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•œ๋‹ค.
  • QColorDialog ๋ฅผ ์‚ฌ์šฉํ•ด ๊ธ€์ž ์ƒ‰์„ ์„ ํƒํ•˜๊ณ  ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•œ๋‹ค.
  • QMessageBox.about ์„ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•œ ๋„์›€๋ง ์ฐฝ์„ ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•์„ ์ตํžŒ๋‹ค.
  • ๋ฉ”๋‰ด ์•ก์…˜(actionFont, actionColor, actionAbout_App)๊ณผ ์Šฌ๋กฏ ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ํŒจํ„ด์„ ์ตํžŒ๋‹ค.
  • ์„ ํƒ๋œ ํ…์ŠคํŠธ์—๋งŒ ํฐํŠธ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹๊ณผ, ํ–ฅํ›„ ์ž…๋ ฅ๋  ํ…์ŠคํŠธ์— ํฐํŠธ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ดํ•ดํ•œ๋‹ค.

 


 

2. ์ „์ฒด ๋กœ์ง

๋”๋ณด๊ธฐ
# mainwindow.py
 
. . .
 
from PySide6.QtWidgets import QFontDialog, QColorDialog
 
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
 
        . . .
        
        self.ui.actionFont.triggered.connect(self.font_dialog)
        self.ui.actionColor.triggered.connect(self.color_dialog)
        self.ui.actionAbout_App.triggered.connect(self.about)
        
        . . .
 
 	def font_dialog(self):
        ok, font = QFontDialog.getFont(
            self.editor.font(),
            self,
            "ํฐํŠธ ์„ ํƒ"
        )
        if not ok:
            return

        cursor = self.editor.textCursor()
        fmt = QTextCharFormat()
        fmt.setFont(font)

        if cursor.hasSelection():
            cursor.mergeCharFormat(fmt)
        else:
            current_fmt = self.editor.currentCharFormat()
            current_fmt.merge(fmt)
            self.editor.setCurrentCharFormat(current_fmt)

    def color_dialog(self):
        color = QColorDialog.getColor(
            self.editor.textColor(),
            self,
            "๊ธ€์ž ์ƒ‰ ์„ ํƒ"
        )
        if color.isValid():
            self.editor.setTextColor(color)

    def about(self):
        QMessageBox.about(
            self,
            "About Notepad",
            "PySide6 ๋ฉ”๋ชจ์žฅ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.\n"
            "ํฐํŠธ, ์ปฌ๋Ÿฌ, ๋„์›€๋ง ๊ธฐ๋Šฅ์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."
        )
# main.py

import sys
from PySide6.QtWidgets import QApplication

from mainwindow import MainWindow

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec())


3. ํ•„์š”ํ•œ import + ์ฃผ์„ ์„ค๋ช…

๋”๋ณด๊ธฐ
# QFont ๋Š” ๊ธ€๊ผด ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค
# QTextCharFormat ์€ ๊ฐœ๋ณ„ ๋ฌธ์ž(๋˜๋Š” ์˜์—ญ)์˜ ์„œ์‹์„ ๋‹ค๋ฃจ๋Š” ํด๋ž˜์Šค
from PySide6.QtGui import QFont, QTextCharFormat

QFont, QTextCharFormat: ์„ ํƒ ์˜์—ญ์— ํฐํŠธ ์†์„ฑ์„ ์ž…ํžˆ๊ธฐ ์œ„ํ•œ ํ•ต์‹ฌ ํด๋ž˜์Šค


 

4. ๋ฉ”๋‰ด ์•ก์…˜๊ณผ ์Šฌ๋กฏ ์—ฐ๊ฒฐํ•˜๊ธฐ

๋”๋ณด๊ธฐ
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # QTextEdit ๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ณ„์นญ์„ ๋งŒ๋“ค์–ด ๋‘”๋‹ค
        self.editor = self.ui.textEdit

        # 9.13 ๋‹จ๊ณ„: ํฐํŠธ, ์ปฌ๋Ÿฌ, ๋„์›€๋ง ๋ฉ”๋‰ด์— ํ•จ์ˆ˜ ์—ฐ๊ฒฐ
        # ๋ฉ”๋‰ด๋‚˜ ํˆด๋ฐ”์—์„œ actionFont ๊ฐ€ ๋ˆŒ๋ฆฌ๋ฉด font_dialog ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค
        self.ui.actionFont.triggered.connect(self.font_dialog)

        # actionColor ๊ฐ€ ๋ˆŒ๋ฆฌ๋ฉด color_dialog ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค
        self.ui.actionColor.triggered.connect(self.color_dialog)

        # actionAbout_App ์ด ๋ˆŒ๋ฆฌ๋ฉด about ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค
        self.ui.actionAbout_App.triggered.connect(self.about)

 

5. ์‹ค์ œ ๊ธฐ์กด ํŒŒ์ผ ์—ด๊ธฐ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ํ•จ์ˆ˜

๋”๋ณด๊ธฐ

5-1. ํฐํŠธ ์„ ํƒ ๊ธฐ๋Šฅ

    def font_dialog(self):
        # QFontDialog.getFont ๋Š” ๋‘ ๊ฐœ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
        # ์ฒซ ๋ฒˆ์งธ ๊ฐ’ ok   : ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์ด์–ผ๋กœ๊ทธ์—์„œ ํ™•์ธ์„ ๋ˆŒ๋ €๋Š”์ง€ ์—ฌ๋ถ€
        # ๋‘ ๋ฒˆ์งธ ๊ฐ’ font : ์„ ํƒ๋œ QFont ๊ฐ์ฒด
        ok, font = QFontDialog.getFont(
            self.editor.font(),  # ํ˜„์žฌ ์—๋””ํ„ฐ์˜ ํฐํŠธ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ
            self,                # ๋ถ€๋ชจ ์œ„์ ฏ
            "ํฐํŠธ ์„ ํƒ"           # ๋‹ค์ด์–ผ๋กœ๊ทธ ์ œ๋ชฉ
        )

        # ์‚ฌ์šฉ์ž๊ฐ€ ์ทจ์†Œ๋ฅผ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ๋Š” ์ ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค
        if not ok:
            return

        # ํ˜„์žฌ ํ…์ŠคํŠธ ์ปค์„œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
        cursor = self.editor.textCursor()

        # ๋ฌธ์ž ์„œ์‹ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ ๋‹ค
        fmt = QTextCharFormat()

        # ์„ ํƒ๋œ ํฐํŠธ ์ •๋ณด๋ฅผ ๋ฌธ์ž ์„œ์‹์— ์„ค์ •ํ•œ๋‹ค
        fmt.setFont(font)

        if cursor.hasSelection():
            # ํ…์ŠคํŠธ๊ฐ€ ์„ ํƒ๋˜์–ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์„ ํƒ ์˜์—ญ์—๋งŒ ์ƒˆ๋กœ์šด ํฐํŠธ๋ฅผ ์ ์šฉํ•œ๋‹ค
            cursor.mergeCharFormat(fmt)
        else:
            # ์„ ํƒ ์˜์—ญ์ด ์—†๋‹ค๋ฉด ์ดํ›„ ์ž…๋ ฅ๋  ํ…์ŠคํŠธ์˜ ๊ธฐ๋ณธ ๋ฌธ์ž ์„œ์‹์„ ๋ณ€๊ฒฝํ•œ๋‹ค
            current_fmt = self.editor.currentCharFormat()
            current_fmt.merge(fmt)
            self.editor.setCurrentCharFormat(current_fmt)
  • getFont ์˜ ๋ฐ˜ํ™˜ ์ˆœ์„œ๊ฐ€ PySide6 ์—์„œ๋Š” ok, font ์ด๋ผ๋Š” ์ 
  • QTextCharFormat ์„ ์‚ฌ์šฉํ•ด์„œ ์„ ํƒ๋œ ์˜์—ญ์—๋งŒ ํฐํŠธ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ 
  • ์„ ํƒ์ด ์—†์„ ๋•Œ๋Š” currentCharFormat ์„ ๋ณ€๊ฒฝํ•ด ์ดํ›„ ๋ฌธ์ž์˜ ์„œ์‹์„ ๋ฐ”๊พผ๋‹ค๋Š” ์ 

 

 

 

 

5-2. ๊ธ€์ž ์ƒ‰ ์„ ํƒ ๊ธฐ๋Šฅ

    def color_dialog(self):
        # QColorDialog.getColor ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ QColor ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
        # ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋ถ€๋ชจ ์œ„์ ฏ, ์„ธ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ ์ œ๋ชฉ์ด๋‹ค
        color = QColorDialog.getColor(
            self.editor.textColor(),  # ํ˜„์žฌ ํ…์ŠคํŠธ ์ƒ‰์ƒ์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ
            self,
            "๊ธ€์ž ์ƒ‰ ์„ ํƒ"
        )

        # ์‚ฌ์šฉ์ž๊ฐ€ ์ทจ์†Œ๋ฅผ ๋ˆ„๋ฅด๋ฉด invalid ์ƒ‰์ƒ์ด ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ isValid ๋กœ ์ฒดํฌํ•œ๋‹ค
        if not color.isValid():
            return

        # setTextColor ๋Š” ์„ ํƒ๋œ ์˜์—ญ์ด ์žˆ์œผ๋ฉด ๊ทธ ๋ถ€๋ถ„์—๋งŒ ์ ์šฉํ•˜๊ณ 
        # ์„ ํƒ๋œ ์˜์—ญ์ด ์—†์œผ๋ฉด ์ดํ›„์— ์ž…๋ ฅ๋  ํ…์ŠคํŠธ ์ƒ‰์ƒ์— ์˜ํ–ฅ์„ ์ค€๋‹ค
        self.editor.setTextColor(color)
  • ์ƒ‰์ƒ ๋‹ค์ด์–ผ๋กœ๊ทธ์—์„œ ์ทจ์†Œํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋Š” QColor.isValid ๋กœ ํŒ๋‹จ
  • setTextColor ๋Š” ์„ ํƒ ์˜์—ญ์— ์ฆ‰์‹œ ๋ฐ˜์˜๋˜๋ฉฐ, ์„ ํƒ์ด ์—†์œผ๋ฉด ์ปค์„œ ์ดํ›„ ํ…์ŠคํŠธ ์ƒ‰์ƒ์ด ๋ณ€๊ฒฝ๋จ

 

 

 


5-3. ๋„์›€๋ง(About) ๊ธฐ๋Šฅ

    def about(self):
        # QMessageBox.about ์€ ๊ฐ„๋‹จํ•œ ์ •๋ณด ์ฐฝ์„ ์‰ฝ๊ฒŒ ๋„์šฐ๋Š” ์ •์  ๋ฉ”์„œ๋“œ๋‹ค
        # ์ฒซ ๋ฒˆ์งธ ์ธ์ž self  : ๋ถ€๋ชจ ์ฐฝ
        # ๋‘ ๋ฒˆ์งธ ์ธ์ž      : ์ œ๋ชฉ
        # ์„ธ ๋ฒˆ์งธ ์ธ์ž      : ๋‚ด์šฉ
        QMessageBox.about(
            self,
            "About Notepad",
            "PySide6 ๊ธฐ๋ฐ˜ ๋ฉ”๋ชจ์žฅ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.\n\n"
            "์ด ์˜ˆ์ œ์—์„œ๋Š” ํฐํŠธ, ์ƒ‰์ƒ, ์ธ์‡„, PDF ๋‚ด๋ณด๋‚ด๊ธฐ,\n"
            "๋ฌธ์ž ์Šคํƒ€์ผ ๋ฐ ๋ฌธ๋‹จ ์ •๋ ฌ ๊ธฐ๋Šฅ์„ ์—ฐ์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."
        )
  • ๋ณ„๋„์˜ ๋‹ค์ด์–ผ๋กœ๊ทธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„ ๊ฐ„๋‹จํ•œ ์•ˆ๋‚ด์ฐฝ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค
  • ํ…์ŠคํŠธ ์•ˆ์— ์ค„๋ฐ”๊ฟˆ์„ ๋„ฃ์–ด ์—ฌ๋Ÿฌ ์ค„ ์„ค๋ช…์„ ๋ณด์—ฌ ์ค„ ์ˆ˜ ์žˆ๋‹ค

 

6. ์‹คํ–‰


7.ํ•™์Šต ์ฃผ์š” ํฌ์ธํŠธ

๋”๋ณด๊ธฐ

QFontDialog.getFont ์‚ฌ์šฉ๋ฒ•

  • PySide6 ์—์„œ๋Š” ๋ฐ˜ํ™˜ ์ˆœ์„œ๊ฐ€ ok, font ์ด๋ผ๋Š” ์ 
  • ok ๊ฐ€ ์ฐธ์ผ ๋•Œ๋งŒ ์„ ํƒํ•œ ํฐํŠธ๋ฅผ ์ ์šฉํ•ด์•ผ ์•ˆ์ „ํ•˜๋‹ค.

 

QTextCharFormat ์„ ์ด์šฉํ•œ ์„ ํƒ ์˜์—ญ ์„œ์‹ ์ ์šฉ

  • cursor.charFormat ๋งŒ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ๋Š” ๋ฒ”์œ„ ์ ์šฉ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
  • cursor.mergeCharFormat ์„ ์‚ฌ์šฉํ•˜๋ฉด ์„ ํƒ๋œ ์˜์—ญ์—๋งŒ ์„œ์‹์„ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์„ ํƒ ์˜์—ญ์ด ์—†์„ ๋•Œ๋Š” currentCharFormat ์„ ๋ณ€๊ฒฝํ•ด ์ดํ›„ ์ž…๋ ฅ๋  ํ…์ŠคํŠธ์˜ ๊ธฐ๋ณธ ์Šคํƒ€์ผ์„ ๋ฐ”๊พผ๋‹ค.

 

QColorDialog.getColor ์™€ ์ƒ‰์ƒ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

  • ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์ด์–ผ๋กœ๊ทธ์—์„œ ์ทจ์†Œ๋ฅผ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
  • color.isValid ๋กœ ๊ฒ€์‚ฌํ•˜์—ฌ ์œ ํšจํ•œ ์ƒ‰์ƒ์ผ ๋•Œ๋งŒ ์ ์šฉํ•œ๋‹ค.

 

setTextColor ์˜ ๋™์ž‘ ๋ฐฉ์‹

  • ์„ ํƒ๋œ ํ…์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ์™€ ์—†์„ ๋•Œ์˜ ๋™์ž‘์ด ๋‹ค๋ฅด๋‹ค๋Š” ์ ์ด ์ค‘์š”ํ•˜๋‹ค.
  • ์„ ํƒ์ด ์žˆ์œผ๋ฉด ๊ทธ ๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝ, ์—†์œผ๋ฉด ์ปค์„œ ์ดํ›„๋กœ ์ž…๋ ฅ๋˜๋Š” ๊ธ€์ž์— ์˜ํ–ฅ์„ ์ค€๋‹ค.

 

QMessageBox.about ์„ ์ด์šฉํ•œ ๊ฐ„๋‹จ ๋„์›€๋ง

  • ๋ณ„๋„ ์œˆ๋„์šฐ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„ ๋น ๋ฅด๊ฒŒ ํ”„๋กœ๊ทธ๋žจ ์ •๋ณด๋ฅผ ๋ณด์—ฌ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • ์ œ๋ชฉ๊ณผ ๋‚ด์šฉ๋งŒ ๋„˜๊ฒจ์ฃผ๋ฉด ๊น”๋”ํ•œ ์ •๋ณด ์ฐฝ์ด ๋งŒ๋“ค์–ด์ง„๋‹ค.

 

๋‹จ๊ณ„๋ณ„ ์™„์„ฑ ํŒŒ์ผ