本文我们实现文字的字体、颜色设置小案例,效果展示如下:

入门 PyQt6 看过来(案例)06~ 文字设置_pyqt

 1 定义页面面板

class MyWidget(QWidget):
    def __init__(self,parent=None):
        super(MyWidget,self).__init__(parent)
		#面板标题
        self.setWindowTitle("字体颜色测试")
		#面板位置和宽高
        self.setGeometry(300,200,360,200)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

2 定义字体功能

#字体在面板中的按钮
		self.btnFont = QPushButton(self)
        self.btnFont.setText("字体设置")
		#位置
        self.btnFont.move(260,60)
		#宽高
        self.btnFont.resize(60,30)
     	#绑定槽函数
        self.btnFont.clicked.connect(self.getFont)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

实现槽函数getFont

def getFont(self):
		#获取字体设置面板
        font,ok = QFontDialog.getFont()
        if ok:
			#将字体设置给指定的文字
            self.te.setFont(font)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

3 定义颜色功能

#颜色设置按钮
		self.btnColor = QPushButton(self)
        self.btnColor.setText("颜色设置")
        self.btnColor.move(260, 120)
        self.btnColor.resize(60, 30)
		#绑定颜色槽函数
        self.btnColor.clicked.connect(self.getColor)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

实现颜色槽函数getColor:

def getColor(self):
		#获取颜色面板
        color = QColorDialog.getColor()
		将颜色配置给指定文字
        self.te.setTextColor(color)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

4 文字框功能

#定义文本输入框
		self.te = QTextEdit(self)
		#定义默认文字,可修改
        self.te.setText("测试文字")
  • 1.
  • 2.
  • 3.
  • 4.

完整代码如下:

# -*- coding:utf-8 -*-
"""
------------------------------------------------
File Name: 字体颜色对话框.py
Description:
Author: lzq
date:2024-07-25 09:18
------------------------------------------------
"""
import sys

from PyQt6.QtWidgets import QWidget, QPushButton, QTextEdit, QFontDialog, QColorDialog, QApplication

class MyWidget(QWidget):
    def __init__(self,parent=None):
        super(MyWidget,self).__init__(parent)
        self.setWindowTitle("字体颜色测试")
        self.setGeometry(300,200,360,200)
        self.btnFont = QPushButton(self)
        self.btnFont.setText("字体设置")
        self.btnFont.move(260,60)
        self.btnFont.resize(60,30)
        self.btnFont.clicked.connect(self.getFont)

        self.btnColor = QPushButton(self)
        self.btnColor.setText("颜色设置")
        self.btnColor.move(260, 120)
        self.btnColor.resize(60, 30)
        self.btnColor.clicked.connect(self.getColor)
        self.te = QTextEdit(self)
        self.te.setText("测试文字")
    def getFont(self):
        font,ok = QFontDialog.getFont()
        if ok:
            self.te.setFont(font)
    def getColor(self):
        color = QColorDialog.getColor()
        self.te.setTextColor(color)

if __name__=='__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec())
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

 下一篇:文件选择案例