Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python仅在变量更改时打印_Python_Printing_Interface_Command Line Interface - Fatal编程技术网

Python仅在变量更改时打印

Python仅在变量更改时打印,python,printing,interface,command-line-interface,Python,Printing,Interface,Command Line Interface,我正在从事一个涉及命令行界面的小项目。我希望能够打印一行与命令行一样长的字符。我能用计算机得到尺寸 width, height = get_terminal_size() 如果我试图打印与终端宽度一样长的块字符,我可以使用以下方法执行此操作: import os block = unichr(0x2588) while True: os.system('cls') print block*width 但是,由于每次都在绘制和重画行,因此这看起来很不稳定。我试图找到一种只在宽度

我正在从事一个涉及命令行界面的小项目。我希望能够打印一行与命令行一样长的字符。我能用计算机得到尺寸

width, height = get_terminal_size()
如果我试图打印与终端宽度一样长的块字符,我可以使用以下方法执行此操作:

import os
block = unichr(0x2588)
while True:
    os.system('cls')
    print block*width

但是,由于每次都在绘制和重画行,因此这看起来很不稳定。我试图找到一种只在宽度改变时才绘制的方法,但我很难做到这一点。如果有任何帮助,我们将不胜感激。

请检查宽度是否已更改

while True:
    os.system('cls')
    if width!=last_width:
        print block*width
        last_width = width

通常,您的循环看起来像:while1{cls;print foo;flush output;sleep 1}。这应该足以解决您看到的问题。@Claris我不确定您的意思您需要一份睡眠声明。此外,作为cls的替代方案,您还可以执行以下操作:打印“\rfoo”,@clris,您需要从“未来”导入大括号。