@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试字体颜色功能的简单脚本
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
|
||||
|
||||
from PyQt5.QtWidgets import QApplication, QTextEdit, QPushButton, QVBoxLayout, QWidget, QColorDialog
|
||||
from PyQt5.QtGui import QColor, QTextCharFormat, QTextCursor
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
def test_color_function():
|
||||
"""测试字体颜色功能"""
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
# 创建主窗口
|
||||
window = QWidget()
|
||||
window.setWindowTitle("字体颜色测试")
|
||||
window.setGeometry(100, 100, 400, 300)
|
||||
|
||||
# 创建文本编辑器
|
||||
text_edit = QTextEdit()
|
||||
text_edit.setPlainText("这是一段测试文本。请选择这段文字并点击颜色按钮来更改颜色。")
|
||||
|
||||
# 创建颜色按钮
|
||||
color_btn = QPushButton("选择颜色")
|
||||
|
||||
def change_color():
|
||||
"""更改字体颜色"""
|
||||
color = QColorDialog.getColor(Qt.black, window, "选择字体颜色")
|
||||
if color.isValid():
|
||||
cursor = text_edit.textCursor()
|
||||
if cursor.hasSelection():
|
||||
# 如果有选中文本,只更改选中文本的颜色
|
||||
fmt = cursor.charFormat()
|
||||
fmt.setForeground(color)
|
||||
cursor.setCharFormat(fmt)
|
||||
print(f"已更改选中文本颜色为: {color.name()}")
|
||||
else:
|
||||
# 如果没有选中文本,更改整个文档的默认颜色
|
||||
text_edit.setTextColor(color)
|
||||
print(f"已更改默认文本颜色为: {color.name()}")
|
||||
|
||||
color_btn.clicked.connect(change_color)
|
||||
|
||||
# 布局
|
||||
layout = QVBoxLayout()
|
||||
layout.addWidget(text_edit)
|
||||
layout.addWidget(color_btn)
|
||||
|
||||
window.setLayout(layout)
|
||||
window.show()
|
||||
|
||||
print("字体颜色测试窗口已打开")
|
||||
print("操作说明:")
|
||||
print("1. 在文本编辑器中选择一些文字")
|
||||
print("2. 点击'选择颜色'按钮")
|
||||
print("3. 选择颜色并确认")
|
||||
print("4. 观察选中文本的颜色是否改变")
|
||||
|
||||
sys.exit(app.exec_())
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_color_function()
|
||||
@ -0,0 +1,78 @@
|
||||
# 字体颜色功能测试脚本 - 验证保留之前内容的颜色
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QApplication, QTextEdit, QPushButton, QVBoxLayout, QWidget, QColorDialog
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QColor, QTextCharFormat, QFont
|
||||
|
||||
class ColorTestWindow(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setWindowTitle("字体颜色功能测试 - 保留之前内容的颜色")
|
||||
self.setGeometry(100, 100, 600, 400)
|
||||
|
||||
layout = QVBoxLayout()
|
||||
|
||||
# 创建文本编辑器
|
||||
self.text_edit = QTextEdit()
|
||||
self.text_edit.setPlainText("这是一段测试文本。\n这行文字将保持原有颜色。\n新输入的文字将使用新颜色。")
|
||||
|
||||
# 设置一些初始颜色来模拟原有内容
|
||||
cursor = self.text_edit.textCursor()
|
||||
cursor.select(cursor.Document)
|
||||
cursor.setPosition(0)
|
||||
|
||||
# 给第一行设置红色
|
||||
cursor.movePosition(cursor.StartOfLine)
|
||||
cursor.movePosition(cursor.EndOfLine, cursor.KeepAnchor)
|
||||
fmt = QTextCharFormat()
|
||||
fmt.setForeground(QColor("red"))
|
||||
cursor.setCharFormat(fmt)
|
||||
|
||||
# 给第二行设置蓝色
|
||||
cursor.movePosition(cursor.NextBlock)
|
||||
cursor.movePosition(cursor.EndOfLine, cursor.KeepAnchor)
|
||||
fmt.setForeground(QColor("blue"))
|
||||
cursor.setCharFormat(fmt)
|
||||
|
||||
layout.addWidget(self.text_edit)
|
||||
|
||||
# 创建颜色选择按钮
|
||||
self.color_btn = QPushButton("选择新颜色(保留原有内容颜色)")
|
||||
self.color_btn.clicked.connect(self.on_color_clicked)
|
||||
layout.addWidget(self.color_btn)
|
||||
|
||||
# 创建说明标签
|
||||
self.info_label = QLabel("点击按钮选择颜色,新输入的文本将使用新颜色,原有内容颜色保持不变")
|
||||
layout.addWidget(self.info_label)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def on_color_clicked(self):
|
||||
"""字体颜色按钮点击处理 - 保留之前内容的颜色"""
|
||||
# 显示颜色选择对话框,默认使用当前文本颜色
|
||||
current_color = self.text_edit.textColor()
|
||||
color = QColorDialog.getColor(current_color, self, "选择字体颜色")
|
||||
|
||||
if color.isValid():
|
||||
# 只设置后续输入的默认颜色,不影响已有内容
|
||||
self.text_edit.setTextColor(color)
|
||||
|
||||
# 如果有选中文本,提示用户颜色只对新输入生效
|
||||
cursor = self.text_edit.textCursor()
|
||||
if cursor.hasSelection():
|
||||
self.info_label.setText("字体颜色已设置,新输入的文本将使用该颜色(原有内容颜色保持不变)")
|
||||
else:
|
||||
self.info_label.setText(f"新颜色已设置: {color.name()},后续输入将使用该颜色")
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
window = ColorTestWindow()
|
||||
window.show()
|
||||
|
||||
print("测试说明:")
|
||||
print("1. 文本编辑器中有三行文字,第一行红色,第二行蓝色,第三行默认黑色")
|
||||
print("2. 点击颜色选择按钮选择新颜色")
|
||||
print("3. 在文本末尾输入新文字,观察新文字使用新颜色而原有文字颜色不变")
|
||||
print("4. 测试验证:保留之前内容的颜色功能是否正常工作")
|
||||
|
||||
sys.exit(app.exec_())
|
||||
Loading…
Reference in new issue