0

I'm a novice attempting to write a Z-machine interpreter -- a specialized virtual machine for text adventures like Zork -- in Python, using PyQt4. I decided to subclass QTextEdit for the program's main interface, but I'm having trouble turning it into a widget that will do what I'm looking for. I have to be able to append text to it, accept user input on the same line as the last appended character, then append more text, and the user must never be allowed to edit any of the text previously appended by the program or entered by the user. To put it another way, I have to periodically make all text in the widget read only except for new text the user is typing at the end of it. Here's the code I've tried most recently:

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
  • I am considering displaying a QLineEdit overlayed on a QTextBrowser, then replacing this with text appended to the QTextBrowser upon return. – lunasspecto Feb 07 '14 at 04:51
  • I finally realized I could search for keywords in all public Python code on Github and found [a QTextEdit subclass by Karel Klíč](https://github.com/karelklic/flashfit/blob/1a2393cb2fb3e44d2e34bf568386d9d4a9b22148/gui_console.py) that does pretty much exactly what I'm trying to do. – lunasspecto Feb 10 '14 at 04:39

1 Answers1

0

Could you have two text windows: one read-only for the text already written, and one where the user can type new text? THen when the press enter the text is interpreted, and if your program can use it, the text gets appended to the read-only text widget.

Oliver
  • 27,510
  • 9
  • 72
  • 103
  • I would do that except that it's non-standard behavior for the VM, and people using my application would be displeased. – lunasspecto Feb 06 '14 at 17:36