diff --git a/pages/chat_list.py b/pages/chat_list.py index 5dfae77..b7187d4 100644 --- a/pages/chat_list.py +++ b/pages/chat_list.py @@ -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"

代码执行完成,执行结果


{res}") + self.new_response(f"

代码执行完成,执行结果


{res}") + 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): diff --git a/pages/chat_page.py b/pages/chat_page.py index b69bab5..873c5b9 100644 --- a/pages/chat_page.py +++ b/pages/chat_page.py @@ -168,6 +168,36 @@ class ChatPage(QMainWindow, interface_ui): self.chat_list.new_response( "你好,我叫智子,你的智能Agent助手!

你可以输入“/”搜索行为,或者可有什么要求可以随时吩咐!" ) + self.chat_list.new_response( + "你好,我叫智子,你的智能Agent助手!

你可以输入“/”搜索行为,或者可有什么要求可以随时吩咐!" + ) + +# 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):