You have three possible solutions:
- reprint the whole screen as you want it displayed after user input
- create a GUI, where you have a lot more control over what is displayed
- if on Unix you can try using
curses
Since, you've already tried tkinter, then the GUI route seems a no-go route for you currently. So you may have similar problems with curses as well. As such, I'd recommend sticking to just reprinting your screen as you want it. You'll end up with a history of previous boards and guesses, but it's an easy way to control what the screen looks like.
eg.
def mainloop(board, words, time_limit):
correct_guesses = []
print_board(board)
while not solved(words, correct_guesses) and now() < time_limit:
word = get_user_guess()
if word in words and word not in correct_guesses:
correct_guesses.append(word)
print('correct!\n')
else:
print('wrong!\n')
if solved(words, correct_guesses):
print('complete!')
else:
print('times up!')
You'll end up with something like:
E B C D
C F G H
I J K L
M N O P
your guess: mouse
wrong!
E B C D
C F G H
I J K L
M N O P
your guess: mice
correct!
complete!
If you want to try curses
here is a basic script to get you started:
import curses
def main(stdscr):
board = [
['E', 'B', 'C', 'D'],
['C', 'F', 'G', 'H'],
['I', 'J', 'K', 'L'],
['M', 'N', 'O', 'P'],
]
curses.echo() # we want to see user input on screen
user_guess_prompt = 'your guess: '
guess_line = len(board) + 1 + 2
guess_column = len(user_guess_prompt)
max_guess_size = 15
guess = None
while guess != 'q':
# clear the entire screen
stdscr.clear()
# a bit of help on how to quit (unlike print, we have to add our own new lines)
stdscr.addstr('enter q as a guess to exit\n\n')
# print the board
for line in board:
stdscr.addstr(' '.join(line)+'\n')
# tell the use about their previous guess
if guess == 'mice':
stdscr.addstr(guess + ' is correct!\n')
elif guess is None:
stdscr.addstr('\n')
else:
stdscr.addstr(guess + ' is wrong!\n')
# prompt for the user's guess
stdscr.addstr(user_guess_prompt)
# tell curses to redraw the screen
stdscr.refresh()
# get user input with the cursor initially placed at the given line and column
guess = stdscr.getstr(guess_line, guess_column, max_guess_size).decode('ascii')
if __name__ == '__main__':
curses.wrapper(main)