Stephan K.
Stephan K.

Reputation: 15702

Python program to take user input and refresh console without "scrolling"

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.

Upvotes: 1

Views: 1436

Answers (4)

Elipzer
Elipzer

Reputation: 911

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.

Upvotes: 1

xvan
xvan

Reputation: 4855

Using VT100 control codes:

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

There must be something similar for windows.

Upvotes: 1

W33P
W33P

Reputation: 11

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?

Upvotes: 1

Łukasz Lalik
Łukasz Lalik

Reputation: 61

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.

Upvotes: 1

Related Questions