更改输入光标/指针的X和Y位置

时间:2019-09-02 17:37:46

标签: python-3.x input coordinates cursor-position colorama

所以,我在这里有以下代码:

from colorama import Fore, Back, Style
from colorama import init
from termcolor import colored
init()

print(Fore.RED + "Command >> ")  ## Red-colored print statement
print(Style.RESET_ALL)   ## Reseting the colors back to default (so the input won't be red aswell)
print("\033[%d;%dH" % (1, 1))   ## Changing X and Y pos
inp = input()  ## Getting Input

正如您所看到的,我正在尝试print红色的“ Command >>”,并将input的坐标更改为位于打印语句的前面。但是,它不起作用,input始终停留在已打印的声明下。我选择了Colorama模块,因为它有助于打印颜色等。

如果有人不理解或不想详细说明上述问题,我在这里。

1 个答案:

答案 0 :(得分:0)

我终于找到答案了!这是有人好奇的地方:

import sys
from colorama import Fore, Back, Style
from colorama import init
init()

def printPos(x, y, text_to_print):   #Function that let us print in desired Position
    sys.stdout.write("\x1b[%d;%df%s" % (x, y, text_to_print))
    sys.stdout.flush()

print(Fore.RED + "Command >>")   #Red-colored print statement
printPos(1, 11, " ")   #Changing pos to 1, 11
inp = input()    #Getting the input