I'm currently writing a small text-based game, mainly for the purposes of learning how to use Curses within python. However, I've run into a problem with the curses.panel module. When creating two panels from windows which do not overlap, the hide() and show() functions for each panel work independently, but when combined together work intermittently.
For an example, I have included a minimum working (or rather, not working) example below. To reproduce, press 1 to show window one, press 2 to show window two, then 2 again to hide window two. This last step results in both windows being hidden, and subsequent presses of 2 show and hide both windows simultaneously. This was tested using python version 3.3.
import curses
import curses.panel
def main(stdscr):
# Setup screen object
curses.cbreak() # No need for [Return]
curses.noecho() # Stop keys being printed
curses.curs_set(0) # Invisible cursor
stdscr.keypad(True)
stdscr.clear()
# format: (lines, cols, y, x)
window_one = curses.newwin(10, 20, 1, 1)
window_two = curses.newwin(5, 20, 5, 40)
# Make windows clearly visible
window_one.addstr(2, 2, "Window One")
window_one.border(0)
window_two.addstr(2, 2, "Window Two")
window_two.border(0)
# Create panels
panel_one = curses.panel.new_panel(window_one)
panel_two = curses.panel.new_panel(window_two)
# Both hidden by default
display_one = False
display_two = False
while True:
if display_one:
window_one.refresh()
panel_one.show()
else:
panel_one.hide()
if display_two:
window_two.refresh()
panel_two.show()
else:
panel_two.hide()
stdscr.refresh()
key = stdscr.getkey()
if key == '1':
display_one = not display_one
elif key == '2':
display_two = not display_two
elif key == 'q':
return
if __name__ == "__main__":
curses.wrapper(main)
Found the problem! To anyone else who's having this problem: I just had to put the line
curses.panel.update_panels()
after the two if-else blocks -- i.e. the blocks of code which altered the visibility of the panels.