PyQt:作为QTextEdit子类的控制台类接口

2024-06-07 04:57:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个新手,尝试使用PyQt4在Python中编写Z-machine解释器——一个专门用于像Zork这样的文本冒险的虚拟机。我决定将QTextEdit作为程序主界面的子类,但是我很难将它转换成一个小部件来实现我所寻找的功能。我必须能够附加文本到它,接受用户输入在同一行作为最后一个附加字符,然后附加更多的文本,并且永远不能允许用户编辑之前由程序附加或用户输入的任何文本。换句话说,我必须周期性地使widget中的所有文本都是只读的,除了对于用户在其末尾输入的新文本。以下是我最近尝试的代码:

class ZScreen(QTextEdit):
    def __init__(self,  parent=None):
        super(QTextEdit, self).__init__(parent)
        self.setUndoRedoEnabled(False)
        self.setAcceptRichText(False)
        self.readOnlyBefore = self.textCursor().position
    def changeEvent(self, e):
        if self.textCursor().position < self.readOnlyBefore:
            self.setReadOnly(True)
        else:
            self.setReadOnly(False)
        super(QTextEdit, self).changeEvent(e)
    def printTo(self, text):
        self.append(text)
        self.moveCursor(QTextCursor.End)
        self.readOnlyBefore = self.textCursor().position

Tags: 用户文本self程序falseinitdefposition
1条回答
网友
1楼 · 发布于 2024-06-07 04:57:37

你能有两个文本窗口吗:一个是已写文本的只读窗口,另一个是用户可以输入新文本的窗口?然后,当按enter键时,文本被解释,如果您的程序可以使用它,文本将被附加到只读文本小部件中。在

相关问题 更多 >