Python hide already printed text

白昼怎懂夜的黑 提交于 2021-01-28 14:28:19

问题


I'm creating a simple two-player board game where each player must place pieces on their own boards. What I would like to do is by either:

  • opening a new terminal window (regardless which OS the program is run on) for both players so that the board is saved within a variable but the other player cannot scroll up to see where they placed their pieces.

  • clearing the current terminal completely so that neither player could scroll and see the other player's board. I am aware of the unix 'clear' command but it doesn't achieve the effect I'm after and doesn't work with all OS's (though this might be something that I'll have to sacrifice to get a working solution)

I have tried clearing the screen but haven't been able to completely remove all the text. I don't have a preference; whichever method is easier. Also, if it would be easier to use a different method that I haven't thought of, all other suggestions are welcome. Thanks in advance!

EDIT: Other solutions give the appearance that text has been cleared but a user could still scroll up and see the text that was cleared. I'd like a way to remove any way that a user could see this text.

EDIT 2: Please read the other answers and the comments as they provide a lot of information about the topic as a whole. In particular, thanks to @zondo.


回答1:


I would recomend a simple ANSI escape code to move the cursor position, Cursor Escape Codes, to the start of the board everytime. There is also an ANSI escape code that completly clears the console though, so you can choose.

If you are on windows you must first import colorama a module that makes windows prompt be able to use the ANSI codes as such:

import colorama   # OR: from colorama import init
colorama.init()   # AND THEN: init()

So if your board has n rows, after the user input for their turn, you move the cursor UP n rows + however many were required for user input, so if you wrote Input row, col: ... then you would go UP n+1, etc...

A simple example:

numLines = 1
print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")



回答2:


Consider using a portable terminal handling library. They abstract away the system specifica of common tasks like erasing the "screen" (i.e. terminal), or placing output at a specific position on the "screen" (again, meaning the text terminal). However, to use such a library effectively, you often have to switch to its style of generating output on the screen instead of naively printing strings.

curses is one such library (based on the C library ncurses) and included in the Python standard library. To get started, be sure to have a look at the curses tutorial in the official Python documentation.




回答3:


I'd personally just use this.

import os
os.system("cls" if os.name == "nt" else "clear") #"cls" for Windows, otherwise "clear"



回答4:


You may not like this, it's a bit higher level than a basic two player board game, but there is always using some sort of GUI. I personally like tkinter myself.

You don't want the option of people scrolling up to see printed text, but you can't remove what has been printed, that's like asking a printer to remove ink off a page. It's going to stay there.

Research a GUI interface, and try and make the game in that. Otherwise, you could let me take a stab at creating a explanatory piece of code that shows you how to use tkinter. If you do, link me the game you have so I can understand what you want.



来源:https://stackoverflow.com/questions/35813667/python-hide-already-printed-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!