TextEdit问题求教~~
本帖最后由 凊风 于 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输出内容,且把用户录入内容储存起来,请问需要如何处理?
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()) sfqxx 发表于 2024-3-17 12:40
谢谢,我测试一下。 sfqxx 发表于 2024-3-17 12:40
报错了这句:
TypeError: WindowB.handleTextChanged() missing 1 required positional argument: 'handleTextChanged'
请问是什么意思? 凊风 发表于 2024-3-17 13:30
报错了这句:
TypeError: WindowB.handleTextChanged() missing 1 required positional argument: 'hand ...
Ai怎么不理我了呢?
页:
[1]