修复(chat_list.py):修复在chat_list.py中对QTextEdit和运行按钮的样式设置问题

🐛 修复(chat_list.py):解决代码执行完成后执行结果样式显示问题
📝 更新(chat_list.py):更新代码注释和文档说明
📝 更新(chat_page.py):更新欢迎消息的内容说明和注释部分的代码展示
🔧 更新(chat_list.py):更新对QTextEdit的样式设置,包括背景颜色和字体大小
This commit is contained in:
yuruo
2024-06-03 20:26:23 +08:00
parent 91705678e1
commit a0269fa9bd
2 changed files with 60 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
import traceback
from PyQt6.QtCore import Qt, QThread, pyqtSignal
from PyQt6.QtWidgets import QTextEdit, QPushButton, QLabel,QAbstractItemView, QListWidget, QSpacerItem, QSizePolicy, QListWidgetItem
from PyQt6.QtWidgets import QApplication, QTextEdit, QPushButton, QLabel,QAbstractItemView, QListWidget, QSpacerItem, QSizePolicy, QListWidgetItem
from PyQt6.QtGui import QPixmap
from PyQt6 import QtGui, QtCore, QtWidgets
from agent.programmer_agent import ProgrammerAgent
@@ -65,6 +65,11 @@ class ChatList(QListWidget):
highlight_current_line=True,
syntax_high_lighter=PythonHighlighter,
)
self.text_edit.setStyleSheet("""
background-color: white;
border-radius: 10px;
font-size:16px;
""")
text = text.strip('```python').rstrip('```')
self.text_edit.setPlainText(text)
# 设置 widget、v_box 和 item 的大小
@@ -82,21 +87,25 @@ class ChatList(QListWidget):
run_button.clicked.connect(lambda:self.run_button_clicked(self.text_edit.toPlainText()))
run_button.setStyleSheet("background-color: green; color: white;")
run_button_h_box.addWidget(run_button)
run_button_h_box.setStretch(2, 4)
run_button_h_box.setStretch(1, 4)
conversation_box.addLayout(run_button_h_box)
def update_size(widget, item):
# 获取 QTextEdit 的文档的大小
doc_size = self.text_edit.document().size().toSize()
conversation_widget.setFixedHeight(doc_size.height()*25 + 20)
conversation_item.setSizeHint(conversation_widget.size()) # # 设置 QTextEdit 的背景为白色,边角为椭圆
self.text_edit.document().documentLayout().documentSizeChanged.connect(lambda: update_size(conversation_widget, conversation_item))
# 获取 QTextEdit 的文档的大小
doc_size = self.text_edit.document().size().toSize()
conversation_widget.setFixedHeight(doc_size.height()*25 + 20)
conversation_item.setSizeHint(conversation_widget.size()) # # 设置 QTextEdit 的背景为白色,边角为椭圆
# 获取 QTextEdit 的文档的大小
doc_size = self.text_edit.document().size().toSize()
print("doc_size", doc_size)
conversation_widget.setFixedHeight(doc_size.height()*25 + 20)
conversation_item.setSizeHint(conversation_widget.size())
def run_button_clicked(self, text):
self.new_response("执行代码中...")
res = PythonExecute().run(text)
self.takeItem(self.count()-1)
self.new_response(f"<p style='color:green;font-size:14px;'>代码执行完成,执行结果</p><br><code>{res}</code>")
self.new_response(f"<p style='color:green;'>代码执行完成,执行结果</p><br><code>{res}</code>")
def _text_response_render(self, text, conversation_widget, conversation_box, conversation_item):
self.text_edit = QTextEdit()
@@ -105,9 +114,15 @@ class ChatList(QListWidget):
def update_size(widget, item):
# 获取 QTextEdit 的文档的大小
doc_size = self.text_edit.document().size().toSize()
print("doc_size", doc_size.height())
# 设置 widget、v_box 和 item 的大小
widget.setFixedHeight(doc_size.height() + 55)
item.setSizeHint(widget.size())
self.text_edit.setStyleSheet("""
background-color: white;
border-radius: 10px;
font-size:14px;
""")
self.text_edit.document().documentLayout().documentSizeChanged.connect(lambda: update_size(conversation_widget, conversation_item))
conversation_box.addWidget(self.text_edit)
@@ -118,24 +133,15 @@ class ChatList(QListWidget):
conversation_box = QtWidgets.QVBoxLayout(conversation_widget)
conversation_box.addLayout(self._sender_render(role))
conversation_item = QListWidgetItem()
if type=="text":
self._text_response_render(text, conversation_widget, conversation_box, conversation_item)
self.text_edit.setStyleSheet("""
background-color: white;
border-radius: 10px;
font-size:14px;
""")
elif type=="code":
self._code_response_render(text, conversation_widget, conversation_box, conversation_item)
self.text_edit.setStyleSheet("""
background-color: white;
border-radius: 10px;
font-size:16px;
""")
self.text_edit.document().setDocumentMargin(10) # 将 item 添加到 QListWidget
self.insertItem(self.count(), conversation_item)
self.setItemWidget(conversation_item, conversation_widget)
if type=="text":
self._text_response_render(text, conversation_widget, conversation_box, conversation_item)
elif type=="code":
self._code_response_render(text, conversation_widget, conversation_box, conversation_item)
self.text_edit.document().setDocumentMargin(10) # 将 item 添加到 QListWidget
self.scrollToBottom()
def stream_response(self, stream):

View File

@@ -168,6 +168,36 @@ class ChatPage(QMainWindow, interface_ui):
self.chat_list.new_response(
"<b>你好我叫智子你的智能Agent助手</b><br><br>你可以输入“/”搜索行为,或者可有什么要求可以随时吩咐!"
)
self.chat_list.new_response(
"<b>你好我叫智子你的智能Agent助手</b><br><br>你可以输入“/”搜索行为,或者可有什么要求可以随时吩咐!"
)
# self.chat_list.new_response("""
# import os # 导入os库以进行操作系统相关操作
# # [步骤1] 获取当前操作系统用户的主目录路径
# home_path = os.path.expanduser('~')
# # [步骤2] 在用户主目录路径中定位到桌面路径
# if os.name == 'nt': # Windows系统
# desktop_path = os.path.join(home_path, 'Desktop')
# else: # macOS或Linux系统
# desktop_path = os.path.join(home_path, 'Desktop')
# # [步骤3] 在桌面路径中查找文件名为“a.txt”的文件
# file_path = os.path.join(desktop_path, 'a.txt')
# # [步骤4] 打开找到的“a.txt”文件
# try:
# with open(file_path, 'r') as file: # 使用with语句安全打开文件
# # [步骤5] 读取文件“a.txt”的内容并将其展示给用户
# content = file.read() # 读取文件内容
# print(content) # 打印读取的内容
# except FileNotFoundError:
# print("文件未找到请确保文件a.txt在桌面上。")
# except Exception as e:
# print(f"打开文件时遇到错误: {e}")
# """, type="code")
def setup_up(self):