title: How do you combine multiple TUI forms to write more complex applications?
body: <p>I would like to write a program with a <strong>T</strong>ext-based <strong>U</strong>ser <strong>I</strong>nterface (<a href="https://en.wikipedia.org/wiki/Text-based_user_interface" rel="nofollow noreferrer">TUI</a>) that consists of several forms.</p>
  
  <p><a href="https://i.stack.imgur.com/k0umr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k0umr.png" alt="Presentation of the several forms."></a></p>
  
  <ul>
  <li>The first form contains a "list". Each list element represents a button. </li>
  <li>If the respective button is pressed, another form should appear in which one can enter the data for the list entry.</li>
  <li>Then the first form is displayed again (with updated list entries).</li>
  </ul>
  
  <p>Here is my attempt, which uses the library <a href="https://npyscreen.readthedocs.io/introduction.html" rel="nofollow noreferrer">npyscreen</a> but does not return to the first form. The code does also not contain the logic to change the list item.</p>
  
  <pre class="lang-py prettyprint-override"><code>#! /usr/bin/env python3
  # coding:utf8
  
  import npyscreen
  
  # content
  headers = ["column 1", "column 2", "column 3", "column 4"]
  entries = [["a1", "a2", "a3", "a4"],
             ["b1", "b2", "b3", "b4"],
             ["c1", "c2", "c3", "c4"],
             ["d1", "d2", "d3", "d4"], 
             ["e1", "e2", "e3", "e4"]]
  
  
  # returns a string in which the segments are padded with spaces.
  def format_entry(entry):
      return "{:10} | {:10} | {:10} | {:10}".format(entry[0], entry[1] , entry[2], entry[3])
  
  
  class SecondForm(npyscreen.Form):
      def on_ok(self):
          self.parentApp.switchFormPrevious()
  
      # add the widgets of the second form
      def create(self):
          self.col1 = self.add(npyscreen.TitleText, name="column 1:")
          self.col2 = self.add(npyscreen.TitleText, name="column 2:")
          self.col3 = self.add(npyscreen.TitleText, name="column 3:")
          self.col4 = self.add(npyscreen.TitleText, name="column 4:")
  
  
  class MainForm(npyscreen.Form):    
      def on_ok(self):
          self.parentApp.switchForm(None)
  
      def changeToSecondForm(self):
          self.parentApp.change_form("SECOND")
  
      # add the widgets of the main form
      def create(self):
          self.add(npyscreen.FixedText, value=format_entry(headers), editable=False, name="header")
  
          for i, entry in enumerate(entries):
              self.add(npyscreen.ButtonPress, when_pressed_function=self.changeToSecondForm, name=format_entry(entry))
  
  
  class TestTUI(npyscreen.NPSAppManaged):
      def onStart(self):
          self.addForm("MAIN", MainForm)
          self.addForm("SECOND", SecondForm, name="Edit row")
  
      def onCleanExit(self):
          npyscreen.notify_wait("Goodbye!")
  
      def change_form(self, name):
          self.switchForm(name)
  
  
  if __name__ == "__main__":
      tui = TestTUI()
      tui.run()
  </code></pre>