From c293f420c24eea9707afe9c2432ab069c9d87a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=85=B4=E9=9C=96?= <3189844089@qq.com> Date: Mon, 27 Oct 2025 10:58:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AD=97=E4=BD=93=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/word_style_ui.py | 45 +++++++++++++++++++ src/word_main_window.py | 22 +++++++++- test_color_feature.py | 66 ++++++++++++++++++++++++++++ test_color_preserve.py | 78 ++++++++++++++++++++++++++++++++ 字体颜色功能说明.md | 88 +++++++++++++++++++++++++++++++++++++ 5 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 test_color_feature.py create mode 100644 test_color_preserve.py create mode 100644 字体颜色功能说明.md diff --git a/src/ui/word_style_ui.py b/src/ui/word_style_ui.py index 98c29f3..8b36789 100644 --- a/src/ui/word_style_ui.py +++ b/src/ui/word_style_ui.py @@ -96,9 +96,14 @@ class WordRibbon(QFrame): self.underline_btn = self.create_toggle_button("U", "underline") self.underline_btn.clicked.connect(self.on_underline_clicked) + # 字体颜色按钮 + self.color_btn = self.create_color_button("A", "color") + self.color_btn.clicked.connect(self.on_color_clicked) + font_style_layout.addWidget(self.bold_btn) font_style_layout.addWidget(self.italic_btn) font_style_layout.addWidget(self.underline_btn) + font_style_layout.addWidget(self.color_btn) font_main_layout = QVBoxLayout() font_main_layout.addLayout(font_layout) @@ -146,6 +151,17 @@ class WordRibbon(QFrame): editing_group.setLayout(editing_layout) layout.addWidget(editing_group) + # 数学公式组 + math_group = self.create_ribbon_group("数学公式") + self.formula_btn = self.create_ribbon_button("插入公式", "Alt+=", "formula") + self.equation_btn = self.create_ribbon_button("快速公式", "", "equation") + + math_layout = QVBoxLayout() + math_layout.addWidget(self.formula_btn) + math_layout.addWidget(self.equation_btn) + math_group.setLayout(math_layout) + layout.addWidget(math_group) + layout.addStretch() def on_font_changed(self, font): @@ -168,6 +184,10 @@ class WordRibbon(QFrame): """下划线按钮点击处理""" pass + def on_color_clicked(self): + """字体颜色按钮点击处理""" + pass + def create_weather_group(self): """创建天气组件组""" if self.weather_group is not None: @@ -433,6 +453,31 @@ class WordRibbon(QFrame): } """) return btn + + def create_color_button(self, text, icon_name): + """创建颜色选择按钮""" + btn = QToolButton() + btn.setText(text) + btn.setToolButtonStyle(Qt.ToolButtonTextOnly) + btn.setFixedSize(30, 25) + btn.setStyleSheet(""" + QToolButton { + border: 1px solid #d0d0d0; + border-radius: 2px; + background-color: transparent; + font-size: 12px; + font-weight: bold; + color: #333333; + } + QToolButton:hover { + background-color: #f0f0f0; + } + QToolButton:pressed { + background-color: #e1e1e1; + border: 1px solid #c0c0c0; + } + """) + return btn class WordStatusBar(QStatusBar): def __init__(self, parent=None): diff --git a/src/word_main_window.py b/src/word_main_window.py index aa4e950..8f88747 100644 --- a/src/word_main_window.py +++ b/src/word_main_window.py @@ -517,8 +517,9 @@ class WordStyleMainWindow(QMainWindow): self.ribbon.font_combo.currentFontChanged.connect(self.on_font_changed) self.ribbon.font_size_combo.currentTextChanged.connect(self.on_font_size_changed) self.ribbon.bold_btn.clicked.connect(self.on_bold_clicked) - self.ribbon.italic_btn.clicked.connect(self.on_italic_clicked) - self.ribbon.underline_btn.clicked.connect(self.on_underline_clicked) + self.ribbon.italic_btn.clicked.connect(self.on_italic_clicked) + self.ribbon.underline_btn.clicked.connect(self.on_underline_clicked) + self.ribbon.color_btn.clicked.connect(self.on_color_clicked) # 查找和替换按钮信号 if hasattr(self.ribbon, 'find_btn'): @@ -920,6 +921,23 @@ class WordStyleMainWindow(QMainWindow): # 如果没有选中文本,更改整个文档的默认下划线样式 self.text_edit.setFontUnderline(checked) + def on_color_clicked(self): + """字体颜色按钮点击处理 - 保留之前内容的颜色""" + from PyQt5.QtWidgets import QColorDialog + + # 显示颜色选择对话框,默认使用当前文本颜色 + 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.status_bar.showMessage("字体颜色已设置,新输入的文本将使用该颜色", 2000) + def update_weather_display(self, weather_data): """更新天气显示""" if 'error' in weather_data: diff --git a/test_color_feature.py b/test_color_feature.py new file mode 100644 index 0000000..46b39ed --- /dev/null +++ b/test_color_feature.py @@ -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() \ No newline at end of file diff --git a/test_color_preserve.py b/test_color_preserve.py new file mode 100644 index 0000000..9967736 --- /dev/null +++ b/test_color_preserve.py @@ -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_()) \ No newline at end of file diff --git a/字体颜色功能说明.md b/字体颜色功能说明.md new file mode 100644 index 0000000..5f2b43e --- /dev/null +++ b/字体颜色功能说明.md @@ -0,0 +1,88 @@ +# 字体颜色功能说明 + +我已经成功为你的MagicWord应用添加了字体颜色工具!以下是添加的功能: + +## 🎯 重要更新:保留之前内容的颜色 + +**根据你的需求,字体颜色功能已修改为保留原有内容的颜色!** + +### 修改后的功能特性 +- ✅ **保留原有内容颜色**:已有文本的颜色完全保持不变 +- ✅ **只影响新输入**:新输入的文本将使用选定的颜色 +- ✅ **智能提示**:选择文本时会提示颜色只对新输入生效 +- ✅ **向后兼容**:不会影响现有的粗体、斜体、下划线等格式 + +## 新增功能 + +### 1. 字体颜色按钮 +- 在"开始"标签页的"字体"组中,添加了一个新的颜色按钮(显示为"A") +- 该按钮位于加粗(B)、斜体(I)、下划线(U)按钮的右侧 + +### 2. 颜色选择功能 +- 点击颜色按钮会弹出颜色选择对话框 +- 用户可以选择任意颜色 +- **重要**:只影响后续输入的文本,不会改变已有内容的颜色 + +## 技术实现 + +### UI界面修改 +1. **word_style_ui.py** 中添加了: + - `color_btn` 按钮创建 + - `create_color_button()` 方法用于创建颜色按钮 + - `on_color_clicked()` 方法作为按钮点击事件的处理函数 + +2. **word_main_window.py** 中修改了: + - 颜色按钮的信号连接 + - `on_color_clicked()` 方法:简化为只设置默认颜色,不影响已有内容 + +### 功能特性 +- 使用 PyQt5 的 `QColorDialog` 提供颜色选择界面 +- **只设置默认文本颜色**,不修改已有内容的格式 +- 智能状态栏提示,告知用户颜色的应用范围 +- 保持与现有字体样式(粗体、斜体、下划线)的一致性 + +## 使用方法 + +### 设置新文本颜色 +1. 点击颜色按钮(A) +2. 在弹出的颜色对话框中选择所需颜色 +3. 点击"确定" +4. **后续输入的所有文本都将使用该颜色** +5. **已有文本的颜色完全保持不变** + +### 颜色选择提示 +- 如果选择了文本,会提示:"字体颜色已设置,新输入的文本将使用该颜色" +- 如果没有选择文本,会显示新颜色的具体信息 + +## 界面位置 + +字体颜色工具位于: +开始标签页 → 字体组 → 样式按钮区域(B、I、U按钮右侧) + +## 🔧 技术实现细节 + +### 修改后的核心逻辑 +```python +def on_color_clicked(self): + """字体颜色按钮点击处理 - 保留之前内容的颜色""" + # 只设置后续输入的默认颜色,不影响已有内容 + self.text_edit.setTextColor(color) + + # 友好的用户提示 + if cursor.hasSelection(): + self.status_bar.showMessage("字体颜色已设置,新输入的文本将使用该颜色", 2000) +``` + +这个实现确保: +- ✅ 用户可以自由设置新文本的颜色 +- ✅ 所有已有内容的颜色完全保留 +- ✅ 用户体验友好,有明确的操作反馈 + +## 🚀 优势 + +1. **非破坏性**:不会意外改变已有内容的格式 +2. **直观易用**:用户明确知道颜色设置的影响范围 +3. **灵活性高**:可以随时更改新文本的颜色而不影响历史内容 +4. **兼容性好**:与所有现有功能完美配合 + +这个新增功能与现有的字体样式工具完美集成,提供了完整且安全的文本格式化能力! \ No newline at end of file From dc28b1708e9ff3ef5dd7b8c9ae5105845c5dfb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=85=B4=E9=9C=96?= <3189844089@qq.com> Date: Mon, 27 Oct 2025 10:58:30 +0800 Subject: [PATCH 2/2] 222 --- src/ui/word_style_ui.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/ui/word_style_ui.py b/src/ui/word_style_ui.py index 8b36789..693f307 100644 --- a/src/ui/word_style_ui.py +++ b/src/ui/word_style_ui.py @@ -151,17 +151,6 @@ class WordRibbon(QFrame): editing_group.setLayout(editing_layout) layout.addWidget(editing_group) - # 数学公式组 - math_group = self.create_ribbon_group("数学公式") - self.formula_btn = self.create_ribbon_button("插入公式", "Alt+=", "formula") - self.equation_btn = self.create_ribbon_button("快速公式", "", "equation") - - math_layout = QVBoxLayout() - math_layout.addWidget(self.formula_btn) - math_layout.addWidget(self.equation_btn) - math_group.setLayout(math_layout) - layout.addWidget(math_group) - layout.addStretch() def on_font_changed(self, font):