简体   繁体   中英

addstr delay in Python Curses

I'm working on an AI and I am using Curses and I would like to be able to add a message, wait five seconds then draw another message.

Below is the piece I am trying to fix

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import curses
import time

screen = curses.initscr()
curses.noecho()
curses.curs_set(0)
screen.keypad(1)

screen.addstr("This is a Sample Curses Script\n\n")
screen.addstr("This is a Sample Curses Script\n\n")
time.sleep(5)
screen.addstr("This is a Sample Curses Script\n\n")
while True:
   event = screen.getch()
   if event == ord("q"): break

curses.endwin()

From the official guide :

After you have put on the window what you want there, when you want the portion of the terminal covered by the window to be made to look like it, you must call refresh().

Thus, change your code as such:

import curses
import time

screen = curses.initscr()
curses.noecho()
curses.curs_set(0)
screen.keypad(1)

screen.addstr("This is a Sample Curses Script\n\n")
screen.addstr("This is a Sample Curses Script\n\n")
screen.refresh()
time.sleep(5)
screen.addstr("This is a Sample Curses Script\n\n")
screen.refresh()
while True:
    event = screen.getch()
    if event == ord("q"): break

curses.endwin()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM