1

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
  • 1
    If you don't want to use a library you will be writing a lot of fiddly code. Use a library. Commented Mar 21, 2016 at 23:09
  • 1
    At the very least, check out the code the libraries use to get an idea of how it's done. Commented Mar 21, 2016 at 23:10
  • Checking libraries is a great idea. I figure this problem was already solved somewhere else. Mabye somebody knows without me spending couple of minutes or even hours with Google. Commented Mar 21, 2016 at 23:11
  • Then remove the Ideally from your update. Commented Mar 21, 2016 at 23:11

4 Answers 4

1

Using VT100 control codes:

input("something:")
print( '\x1b[1A\x1b[2K\x1b[1A')
input("something else:")

There must be something similar for windows.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @xvan, these work great for my purposes - ccs.neu.edu/research/gpc/MSim/vona/terminal/…
1

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.

1 Comment

No Libraries! Sorry.
1

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?

Comments

1

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:

#X-Drawer
class Canvas:
    def __init__(self, w, h):
        self.w = w
        self.h = h
        self.c = []
        for i in range(w * h):
            self.c.append(' ')
        print len(self.c)
    def render(self):
        u = 0
        s = ""
        for i in range(len(self.c)):
            s += self.c[i]
            if not i % self.w:
                s += "\n"
        print s
    def drawX(self, x, y):
        n = [(x, y), (x + 1, y + 1), (x + 2, y + 2), (x + 2, y), (x, y + 2)]
        for i in n:
            v = i[0] * self.w + i[1]
            if v < len(self.c):
                self.c[v] = 'X'
    def drawLine(self, x, d):
        n = []
        if d:
            n = [(x, y), (x + 1, y + 1), (x + 2, y + 2)]
        else:
            n = [(x, y), (x + 1, y + 1), (x + 2, y + 2)]
        for i in n:
            v = i[0] * self.w + i[1]
            if v < len(self.c):
                self.c[v] = 'X'

def clearScreen():
    for i in range(64):
        print

c = Canvas(25, 25)

while True:
    clearScreen()
    c.render()
    i = raw_input("Coordinates: ").split()
    c.drawX(int(i[0]), int(i[1]))

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.