title: Updating Multiple NCurses Windows
body: <p>I apologize in advance for a complicated question.</p>
  
  <p>I am using NCurses with Python 3 to make a terminal dashboard. This dashboard has two parts: a logging section and a dictionary display section</p>
  
  <p><a href="https://i.stack.imgur.com/QdZxq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QdZxq.png" alt="enter image description here"></a></p>
  
  <p>The initialization code looks something like this:</p>
  
  <pre><code>panel = None
  window = None
  screen = None
  
  def setup_terminal_logging():
      global panel
      global screen
      global window
      screen = curses.initscr()
      screen.nodelay(1)
      screen.border(0)
  
      maxy, maxx = screen.getmaxyx()
  
      height = 20
      # height, width, begin_y, begin_x
      window = curses.newwin(height, maxx-4, maxy-(height + 1), 2)
  
      panel = curses.newwin(25, maxx-4, 2, 2)
      panel.refresh()
  
      curses.setsyx(-1, -1)
  
      screen.refresh()
      window.refresh()
      window.scrollok(True)
      window.idlok(True)
      window.leaveok(True)
  
      terminal_handler = modules.log.CursesHandler(window)
      logger.addHandler(terminal_handler)
  </code></pre>
  
  <p><code>CursesHandler</code> ties into logging and displays output to the console (bottom section of the screen). It looks something like this:</p>
  
  <pre><code>class CursesHandler(logging.Handler):
      def __init__(self, screen):
          logging.Handler.__init__(self)
          self.screen = screen
  
          curses.start_color()
          curses.use_default_colors()
          for i in range(0, curses.COLORS):
              curses.init_pair(i + 1, i, -1)
  
      def emit(self, record):
          msg = self.format(record)
          screen = self.screen
          ufs = u'\n%s'
          screen.addstr(ufs % msg, 83)
          screen.refresh()
  </code></pre>
  
  <p>And the section at the top is updated by passing two dictionaries in a function and using the globally-scoped <code>window</code> and <code>panel</code> variables:</p>
  
  <pre><code>def update_terminal_display(data, values):
      global panel
      for k, j in enumerate([data, values]):
          for index, (key, value) in enumerate(j.items()):
              if "_locations" in key:
                  continue
              logger.debug("-")
              panel.addstr(index + 2, 2 + (k * 50), str(key) + ": " + str(value))
          panel.refresh()
  </code></pre>
  
  <p>Everything seems to work great, although I've encountered some strange behavior. </p>
  
  <p>If I remove the <code>logger.debug("-")</code> statement in the <code>update_terminal_display</code> method, the display prints out gibberish:</p>
  
  <p><a href="https://i.stack.imgur.com/jJBqH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jJBqH.png" alt="enter image description here"></a></p>
  
  <p>It's strange that literally only removing this statement can cause this behavior, especially when the top panel is separate from the bottom one.</p>
  
  <p>Even more strange, I replaced <code>logger.debug("-")</code> with <code>window.refresh()</code> (which in effect is what it is calling), and it still doesn't work. How can I prevent this behavior from happening? What is causing them to conflict? Can I call a <code>refresh()</code> on one window without it interfering with another?</p>
  
  <p>Any help you could provide would be greatly appreciated!</p>