如何使打印的语句不被 Python 解释器中的用户输入推回?打开新窗口?

如果标题有点含糊,我很抱歉;我不知道如何用语言表达我的担忧。我是 Python 初学者,正在制作 Boggle/Word Search 游戏。

http://img.mukewang.com/610ba4bf000188cf06350301.jpg

我希望董事会留在同一个地方/不被用户输入推回。那可能吗?

我尝试通过记事本.txtPython 中打开一个文件,但是一旦打开 txt 文件,代码就会停止;对此的任何帮助都可以。

我想知道是否也可以让python打开一个新窗口/控制台来显示板,而另一个控制台继续执行原始代码。

最后,是否有一种“刷新”用户在此处输入的方式

一旦用户提交他们的答案,我希望该行为空。我曾尝试使用 tkinter 制作 GUI,但由于我制作的页面数量太多,而且我真的不知道如何使用它,这对我来说太难了。


偶然的你
浏览 140回答 1
1回答

千万里不及你

您有三种可能的解决方案:在用户输入后重新打印您希望显示的整个屏幕创建一个 GUI,您可以在其中更好地控制显示的内容如果在 Unix 上你可以尝试使用&nbsp;curses既然您已经尝试过 tkinter,那么 GUI 路由目前对您来说似乎是一条不可行的路线。所以你可能也有类似的诅咒问题。因此,我建议您坚持只根据需要重新打印屏幕。您最终会得到之前棋盘和猜测的历史记录,但这是一种控制屏幕外观的简单方法。例如。def mainloop(board, words, time_limit):&nbsp; &nbsp; correct_guesses = []&nbsp; &nbsp; print_board(board)&nbsp; &nbsp; while not solved(words, correct_guesses) and now() < time_limit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;word = get_user_guess()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if word in words and word not in correct_guesses:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;correct_guesses.append(word)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print('correct!\n')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print('wrong!\n')&nbsp; &nbsp; if solved(words, correct_guesses):&nbsp; &nbsp; &nbsp; &nbsp; print('complete!')&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print('times up!')你最终会得到类似的东西:E B C DC F G HI J K LM N O Pyour guess: mousewrong!E B C DC F G HI J K LM N O Pyour guess: micecorrect!complete!如果你想尝试curses这里是一个让你开始的基本脚本:import cursesdef main(stdscr):&nbsp; &nbsp; board = [&nbsp; &nbsp; &nbsp; &nbsp; ['E', 'B', 'C', 'D'],&nbsp; &nbsp; &nbsp; &nbsp; ['C', 'F', 'G', 'H'],&nbsp; &nbsp; &nbsp; &nbsp; ['I', 'J', 'K', 'L'],&nbsp; &nbsp; &nbsp; &nbsp; ['M', 'N', 'O', 'P'],&nbsp; &nbsp; ]&nbsp; &nbsp; curses.echo()&nbsp; # we want to see user input on screen&nbsp; &nbsp; user_guess_prompt = 'your guess: '&nbsp; &nbsp; guess_line = len(board) + 1 + 2&nbsp; &nbsp; guess_column = len(user_guess_prompt)&nbsp; &nbsp; max_guess_size = 15&nbsp; &nbsp; guess = None&nbsp; &nbsp; while guess != 'q':&nbsp; &nbsp; &nbsp; &nbsp; # clear the entire screen&nbsp; &nbsp; &nbsp; &nbsp; stdscr.clear()&nbsp; &nbsp; &nbsp; &nbsp; # a bit of help on how to quit (unlike print, we have to add our own new lines)&nbsp; &nbsp; &nbsp; &nbsp; stdscr.addstr('enter q as a guess to exit\n\n')&nbsp; &nbsp; &nbsp; &nbsp; # print the board&nbsp; &nbsp; &nbsp; &nbsp; for line in board:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stdscr.addstr(' '.join(line)+'\n')&nbsp; &nbsp; &nbsp; &nbsp; # tell the use about their previous guess&nbsp; &nbsp; &nbsp; &nbsp; if guess == 'mice':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stdscr.addstr(guess + ' is correct!\n')&nbsp; &nbsp; &nbsp; &nbsp; elif guess is None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stdscr.addstr('\n')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stdscr.addstr(guess + ' is wrong!\n')&nbsp; &nbsp; &nbsp; &nbsp; # prompt for the user's guess&nbsp; &nbsp; &nbsp; &nbsp; stdscr.addstr(user_guess_prompt)&nbsp; &nbsp; &nbsp; &nbsp; # tell curses to redraw the screen&nbsp; &nbsp; &nbsp; &nbsp; stdscr.refresh()&nbsp; &nbsp; &nbsp; &nbsp; # get user input with the cursor initially placed at the given line and column&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; guess = stdscr.getstr(guess_line, guess_column, max_guess_size).decode('ascii')if __name__ == '__main__':&nbsp; &nbsp; curses.wrapper(main)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python