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())
|