Python中的命令行图形

Python中的命令行图形,python,command-line,Python,Command Line,我试图在Windows中编写一个简单的命令行程序,但它看起来不太好。是否有一个库可以生成更接近GUI的CLI 例如: Hello Program 1.0 (bar with white background) Progress Bar: (bar with green background like in Windows OS) Some Labels... Cancel (Button with blue background)

我试图在Windows中编写一个简单的命令行程序,但它看起来不太好。是否有一个库可以生成更接近GUI的CLI

例如:

                         Hello Program 1.0 (bar with white background)

Progress Bar: (bar with green background like in Windows OS)

Some Labels...




Cancel (Button with blue background)                      OK (Button with blue background)

我还无法在互联网上找到解决方案。我不想创建真正的图形窗口,我想使用命令行。

您可以使用诅咒将文本放置在任意位置:

对于Windows变体,您可以使用


请记住,这只允许您将光标定位在窗口上,它不提供更高级别的模块来创建按钮或小部件。

您可以使用光标将文本放置在任何位置:

对于Windows变体,您可以使用


请记住,这只允许您在窗口上定位光标,它不提供更高级别的模块来创建按钮或小部件。

您无法实现在命令行上放置图形元素的目标。无论您是为Windows还是Linux编程,命令行(CLI)应用程序都只能使用非常广泛的元素,如创建粗体、闪烁等的转义序列,以及屏幕上可以输入数据的粗糙区域

如果具有正确的CLI库或工具包,则可以映射控制键,并且可以提示用户输入以下内容

“是否要再次运行此程序(是/否)?”

然后,如果答案为“n”,则程序退出


我会搜索在你的OP上的各种评论中给你的链接和其中一个答案,看看如何在诅咒中编程

您无法实现在命令行上放置图形元素的目标。无论您是为Windows还是Linux编程,命令行(CLI)应用程序都只能使用非常广泛的元素,如创建粗体、闪烁等的转义序列,以及屏幕上可以输入数据的粗糙区域

如果具有正确的CLI库或工具包,则可以映射控制键,并且可以提示用户输入以下内容

“是否要再次运行此程序(是/否)?”

然后,如果答案为“n”,则程序退出


我会搜索在你的OP上的各种评论中给你的链接和其中一个答案,看看如何在诅咒中编程

最后我找到了自己的解决方案。(无按钮:()

看看这段基于termcolor模块和colorama的代码(colorama必须通过pip安装)


你可以试试。多亏菲利普·杜帕诺维奇支持我的问题。

我终于找到了自己的解决方案。(没有按钮可悲:()

看看这段基于termcolor模块和colorama的代码(colorama必须通过pip安装)


你可以试试。多亏Filip Dupanovic支持我的问题。

我不能在windows上使用它。我正在使用windows。请看,从最近的主题指南中,你也可以了解到。我认为你想要的一些控件不能在控制台中使用(如按钮)@aaossa:OP明确指出需要一个仅限CLI的解决方案。是的,它比一个合适的GUI更笨重,但它是可以做到的。我不能在windows上使用它。我正在使用windows。请参阅,从最近的主题指南中,您也可以了解到。我认为您想要的一些控件不能在控制台中使用(如按钮)@aaossa:OP明确表示需要仅使用CLI的解决方案。是的,它比合适的GUI更笨重,但它是可以做到的。与其投票关闭它,我建议您编辑您的原始帖子(OP),然后说你尝试过的,发布少量真实的代码,并展示你对此进行了研究。看看这篇文章,但它看起来不像是好消息:谁在结束这个问题?谁在投票,包括合法的答案?不知道诅咒和问这样的问题有什么不对吗?我在互联网上没有找到任何东西。这是我的公关问题。是的。我不知道诅咒,我希望有一个社区来帮助我。@FilipDupanović我投了票(和其他人一样)关闭此问题。它太宽泛,要求非现场资源,显示OP没有努力,并且他们想做什么也不清楚。这与此网站上出现的问题一样糟糕。与其投票关闭此问题,我建议您编辑您的原始帖子(OP),然后说你尝试过的,发布少量真实的代码,并展示你对此进行了研究。看看这篇文章,但它看起来不像是好消息:谁在结束这个问题?谁在投票,包括合法的答案?不知道诅咒和问这样的问题有什么不对吗?我在互联网上没有找到任何东西。这是我的公关问题。是的。我不知道诅咒,希望社区能帮助我。@FilipDupanović我投票(和其他人一起)结束了这个问题。这个问题太广泛了,需要场外资源,显示OP没有努力,他们想做什么也不清楚。这和这个网站上的问题一样糟糕。谢谢octopusgrabbusThank你octopusgrabbus
import os
import sys
import time
try:
    import colorama
except ImportError:
    raise ImportError("Please install the colorama package.\nYou can do pip install colorama on the command line.")
colorama.init()




# Modules
def cls():
    """ Clears the Screen """
    os.system("cls")

def printpos(text,posy):
    """ Goes to the line [posy] and writes the text there """
    sys.stdout.write("\033["+str(posy)+";1H")
    sys.stdout.write(text)
    sys.stdout.flush()

def title(text):
    """ Sets the title """
    #cls()
    sys.stdout.write("\033[1;1H")
    long = 80
    left = int(int(long - int(len(text))) / 2)
    right = int(int(long - int(len(text))) / 2)
    left = left * " "
    right = right * " "
    sys.stdout.write(cl(left+text+right,"grey","on_white"))
    if str(float(float(len(left+text+right)) / float(2.0)))[-1] == "0":
        None
    else:
        sys.stdout.write(cl(" ","grey","on_white"))
    sys.stdout.flush()


def entry(_title="TITLE",question="QUESTION",clear=True):
    """ Creates an input window. title=window title  question=prompt clear=[True|False] Clear Window before asking(recommended)"""
    if clear:
        cls()
    title(_title)
    lns = len(question.split("\n"))
    middle = 12
    long = 80
    posy = middle - lns
    if lns > 1:
        temp = question.split("\n")[0]
    else:
        temp = question
    posx = int(int(int(long)-int(len(temp))) / 2)
    sys.stdout.write(posy*"\n"+str(posx*" ")+question)
    sys.stdout.flush()
    sys.stdout.write(cl("\n"+str(long*" "),"grey","on_yellow"))
    sys.stdout.write(cl(" ","grey","on_yellow")+str(int(long-2)*" ")+cl(" ","grey","on_yellow"))
    sys.stdout.write(cl(str(long*" "),"grey","on_yellow"))
    sys.stdout.write("\033["+str(posy+lns+3)+";2H")
    sys.stdout.flush()
    ret = raw_input("-> ")
    cls()
    return ret
def waitenter(msg="Please Press Enter ->"):
    """ Entry function like press enter message """
    height = 25
    long = 80
    long = long - 1
    sys.stdout.write("\033["+str(height)+";1H")
    sys.stdout.write(cl(str(long*" "),"grey","on_red"))
    r()
    sys.stdout.write(cl(str(msg),"grey","on_red"))
    raw_input()

ATTRIBUTES = dict(
        list(zip([
            'bold',
            'dark',
            '',
            'underline',
            'blink',
            '',
            'reverse',
            'concealed'
            ],
            list(range(1, 9))
            ))
        )
del ATTRIBUTES['']


HIGHLIGHTS = dict(
        list(zip([
            'on_grey',
            'on_red',
            'on_green',
            'on_yellow',
            'on_blue',
            'on_magenta',
            'on_cyan',
            'on_white'
            ],
            list(range(40, 48))
            ))
        )


COLORS = dict(
        list(zip([
            'grey',
            'red',
            'green',
            'yellow',
            'blue',
            'magenta',
            'cyan',
            'white',
            ],
            list(range(30, 38))
            ))
        )


RESET = '\033[0m'


def colored(text, color=None, on_color=None, attrs=None):
    """Colorize text.

    Available text colors:
        red, green, yellow, blue, magenta, cyan, white.

    Available text highlights:
        on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.

    Available attributes:
        bold, dark, underline, blink, reverse, concealed.

    Example:
        colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
        colored('Hello, World!', 'green')
    """
    if os.getenv('ANSI_COLORS_DISABLED') is None:
        fmt_str = '\033[%dm%s'
        if color is not None:
            text = fmt_str % (COLORS[color], text)

        if on_color is not None:
            text = fmt_str % (HIGHLIGHTS[on_color], text)

        if attrs is not None:
            for attr in attrs:
                text = fmt_str % (ATTRIBUTES[attr], text)

        text += RESET
    return text

def r():
    sys.stdout.write("\r")
    sys.stdout.flush()

returnpos = r
raw_input = input

def cprint(text, color=None, on_color=None, attrs=None, **kwargs):
    """Print colorize text.

    It accepts arguments of print function.
    """

    print((colored(text, color, on_color, attrs)), **kwargs)


cprint("hallo","red","on_white")
cl = colored

if __name__ == "__main__":
    cls()
    title("Hello")
    cprint("That is an example window dialog.\nPython is cool\nWe like it\nIt is so cool.\nIt is awesome.","yellow")
    waitenter()
    cls()
    title("Now Welcome!")
    cprint("In 10 Seconds you are going to see a dialog...","green",end="\r")
    cntr = 10
    for i in range(10):
        r()
        cntr = cntr - 1
        time.sleep(1)
        print(colored("In "+str(cntr)+" Seconds you are going to see a dialog...","green",None,None),end="\r")
    you = entry("Your favourite?","What is your favourite number?")
    cls()
    title("Your favourite number is...")
    cprint(str(you),"magenta")

    waitenter("Please Press Enter to exit -> ")