title: PyQt6: Embed terminal like CMD into TextEdit widget?
body: <p>I'm trying to create a basic terminal in python using PyQt6. The problem is, every single question or tutorial out there uses xterm or something similar to achieve my goal. But, I need the terminal to be cmd itself, embedded inside a QTextEdit widget. Here is my current code:</p>
  <pre><code>class Terminal(QTextEdit):
      def __init__(self, text=&quot;&quot;, defaultInput=&quot;&gt;&quot;):
          super().__init__()
          self.text = text + &quot;\n&quot;
          self.input = defaultInput
          self.quickedit = True
          self.inputs = []
  
          self.append(self.text)
  
      def setInput(self, input):
          self.input = input
          print(self.input)
  
      def _print(self, text):
          self.append(text)
  
      def setCursorPosition(self):
          cursor = QTextCursor(self.document())
          cursor.setPosition(self.document().characterCount() - 1)
          self.setTextCursor(cursor)
  
      def _acceptControl(self):
          self._print(self.input + &quot; &quot;)
          self.setCursorPosition()
  
      def _acceptCommand(self):
          text = self.textCursor().block().text()
          text = text.replace(self.input + &quot; &quot;, &quot;&quot;)
          self.inputs.append(text)
          try:
              self.quickedit = False
              self.callback(text)
              self.quickedit = True
          except NameError:
              pass
          self._acceptControl()
  
      def keyPressEvent(self, event):
          if self.quickedit:
              self.setCursorPosition()
              if event.key() in (16777220, 43):
                  self._acceptCommand()
              else:
                  if event.key() == 16777235:
                      pass
                  elif event.key() == 16777237:
                      pass
                  else:
                      super().keyPressEvent(event)
  
  class Terminal(Terminal):
      def __init__(self):
          self._text = '''
  My Terminal!
          '''
          super().__init__(self._text)
  
          self.process = QProcess()
          self.process.readyReadStandardOutput.connect(self.readOutput)
          self.process.readyReadStandardError.connect(self.readError)
          self.process.finished.connect(self.finished)
          self.process.start(&quot;cmd.exe&quot;)
  
          self.outputs = []
  
      def readOutput(self):
          self._print(self.process.readAllStandardOutput().data().decode(&quot;utf-8&quot;))
          self.outputs.append(self.process.readAllStandardOutput().data().decode(&quot;utf-8&quot;))
  
      def readError(self):
          self._print(self.process.readAllStandardError().data().decode(&quot;utf-8&quot;))
  
      def finished(self):
          self._print(&quot;&lt;div style=\&quot;color:red\&quot;&gt;The process has finished!&lt;/div&gt;&quot;)
          lastline = self.document().lastBlock().text()
          self.setInput(lastline)
      
      def _acceptControl(self):
          self.setCursorPosition()
  
      def callback(self, _input):
          _input = _input.replace(self.outputs[-1], &quot;&quot;)
          self.process.write(_input.encode(&quot;utf-8&quot;))
          self.process.write(&quot;\n&quot;.encode(&quot;utf-8&quot;))
          self.process.waitForBytesWritten(1000)
  </code></pre>
  <p>The problem is, it asks for input, and it responds UNTIL I change the Directory. Then this happens:</p>
  <p><a href="https://i.stack.imgur.com/sdF0C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sdF0C.png" alt="Terminal error" /></a></p>
  <p>Regards.</p>