Let’s say I have this little Python program:
def drawLine():
userInput = input("Coordinates:")
userInput = userInput.split() # x y
# Draw
# ... some drawing algo which will print an "X" on the screen with x,y
drawLine()
drawLine()
Now notice that drawLine()
is called twice, so that two inputs can be taken and two X-es
be drawn. Unfortunately, console will scroll up. I want my python program “to listen” to user key presses and “not scroll away“. Think of a mini-console Photoshop, which also does not scroll your canvas out of sight.
Update:
Problem is small enough to not employ a library.
4
Answers
Probably
Curses
library is what you need in this case.It allows you to display string at given coordinates: https://docs.python.org/3.3/library/curses.html#curses.window.addstr
You can also leave echo mode so you can handle keyboard input as you want without printing it to console: https://docs.python.org/3.3/library/curses.html#curses.noecho.
I’m not sure if I fully understand your question but I think it can be achieve by clearing console and redrawing the ‘frame’. How to clear the interpreter console?
Using VT100 control codes:
There must be something similar for windows.
If it was alright to keep the input below the console, you could use a class to keep your canvas in an array and just render it when you need to.
Here is an example:
You could also replace the clearScreen with an os call to
clr
( How to clear the interpreter console? ) instead of printing 64 lines.Note: My example uses the drawX function, you could use the drawLine function and different coordinates to draw the lines.