13๊ฐ. ํฐํธ / ์ปฌ๋ฌ / ๋์๋ง

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 ์ ์ด์ฉํ ๊ฐ๋จ ๋์๋ง
- ๋ณ๋ ์๋์ฐ ํด๋์ค๋ฅผ ๋ง๋ค์ง ์๊ณ ๋ ๋น ๋ฅด๊ฒ ํ๋ก๊ทธ๋จ ์ ๋ณด๋ฅผ ๋ณด์ฌ ์ค ์ ์๋ค.
- ์ ๋ชฉ๊ณผ ๋ด์ฉ๋ง ๋๊ฒจ์ฃผ๋ฉด ๊น๋ํ ์ ๋ณด ์ฐฝ์ด ๋ง๋ค์ด์ง๋ค.
๋จ๊ณ๋ณ ์์ฑ ํ์ผ
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote




