本文我们实现文字的字体、颜色设置小案例,效果展示如下:
1 定义页面面板
2 定义字体功能
实现槽函数getFont
3 定义颜色功能
实现颜色槽函数getColor:
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.
下一篇:文件选择案例