鱼C论坛

 找回密码
 立即注册
查看: 533|回复: 4

TextEdit问题求教~~

[复制链接]
发表于 2024-3-17 12:29:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 凊风 于 2024-3-17 12:29 编辑

我用的是PySide6,共有3个窗体:

  class BaseWindow(QWidget):

        ......

  class WindowA(BaseWindow):
  
        .....

   class WindowB(BaseWindow):

        .....

          def __init__(self):
                super().__init__()
                        # 设置窗体大小和背景颜色
                self.setFixedSize(1440, 900)
其中WindowB(BaseWindow)窗体有控件self.textEdit_5:
        self.textEdit_5 = QtWidgets.QTextEdit(parent=self)
        self.textEdit_5.setGeometry(QtCore.QRect(480, 250, 921, 295))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setStyleStrategy(QtGui.QFont.StyleStrategy.PreferDefault)
        self.textEdit_5.setFont(font)
        self.textEdit_5.setReadOnly(True)
        self.textEdit_5.setStyleSheet("background-color: rgb(204, 204, 204);")
        self.textEdit_5.setFrameShape(QtWidgets.QFrame.Shape.WinPanel)
        self.textEdit_5.setObjectName("textEdit_5")
        self.textEdit_5.setStyleSheet("font-family:'隶书'; font-size:17pt; line-height: 20px;")

TextEdit_5通过调用self.redirect_output把终端窗口输出内容显示到TextEdit_5中:
     # 重定向输出到TextEdit_5
        self.redirect_output(self.textEdit_5)

    def redirect_output(self, output_widget):
        class StdoutRedirect:
            def __init__(self, widget):
                self.widget = widget

            def write(self, text):
                self.widget.append(text)

            def flush(self):
                pass
        sys.stdout = StdoutRedirect(output_widget)

现需求是:让用户在TextEdit_5输出内容,且把用户录入内容储存起来,请问需要如何处理?



本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-17 12:40:04 | 显示全部楼层
from PySide6 import QtWidgets, QtCore, QtGui
import sys

class WindowB(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        # 设置窗体大小和背景颜色
        self.setFixedSize(1440, 900)

        self.textEdit_5 = QtWidgets.QTextEdit(parent=self)
        self.textEdit_5.setGeometry(QtCore.QRect(480, 250, 921, 295))

        font = QtGui.QFont()
        font.setPointSize(16)
        font.setStyleStrategy(QtGui.QFont.StyleStrategy.PreferDefault)
        
        self.textEdit_5.setFont(font)
        self.textEdit_5.setReadOnly(False)  # Set to False for user to enter input, True before
        self.textEdit_5.setStyleSheet("background-color: rgb(204, 204, 204);")
        self.textEdit_5.setFrameShape(QtWidgets.QFrame.Shape.WinPanel)
        self.textEdit_5.setObjectName("textEdit_5")
        self.textEdit_5.setStyleSheet("font-family:'隶书'; font-size:17pt; line-height: 20px;")
        # 重定向输出到TextEdit_5
        self.redirect_output(self.textEdit_5)
        
        # Connect textChanged signal to handling function
        self.textEdit_5.textChanged.connect(self.handleTextChanged)

    def redirect_output(self, output_widget):
        class StdoutRedirect:
            def __init__(self, widget):
                self.widget = widget

            def write(self, text):
                self.widget.append(text)

            def flush(self):
                pass
            
        sys.stdout = StdoutRedirect(output_widget)

    def handleTextChanged(self):
        text = self.textEdit_5.toPlainText()
        with open('output.txt', 'w') as f:    # Change 'w' to 'a' to append
            f.write(text)

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    window = WindowB()
    window.show()
    sys.exit(app.exec())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 12:43:36 | 显示全部楼层

谢谢,我测试一下。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 13:30:11 | 显示全部楼层

报错了这句:
TypeError: WindowB.handleTextChanged() missing 1 required positional argument: 'handleTextChanged'
请问是什么意思?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 14:47:29 | 显示全部楼层
凊风 发表于 2024-3-17 13:30
报错了这句:
TypeError: WindowB.handleTextChanged() missing 1 required positional argument: 'hand ...

Ai怎么不理我了呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 10:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表