鱼C论坛

 找回密码
 立即注册
查看: 158|回复: 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 | 显示全部楼层
  1. from PySide6 import QtWidgets, QtCore, QtGui
  2. import sys

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

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

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

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

  25.     def redirect_output(self, output_widget):
  26.         class StdoutRedirect:
  27.             def __init__(self, widget):
  28.                 self.widget = widget

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

  31.             def flush(self):
  32.                 pass
  33.             
  34.         sys.stdout = StdoutRedirect(output_widget)

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

  39. if __name__ == "__main__":
  40.     app = QtWidgets.QApplication([])
  41.     window = WindowB()
  42.     window.show()
  43.     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-4-28 04:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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