【问题标题】:How do I delete all of the text on the screen - Python [duplicate]如何删除屏幕上的所有文本-Python [重复]
【发布时间】:2021-02-10 03:48:19
【问题描述】:

我制作定格动画电影,我想看看我是否可以通过编码来做类似的事情。我有这个:

import sleep
print('   _  ')
print('  |_| ')
print('  -|-  ')
print('   |  ')
print('  /\  ')
time.sleep(.1)
print('   _  ')
print('  |_| ')
print('  -|/  ')
print('   |  ')
print('  /\  ')

所以本质上,它看起来就像是简笔画在挥动。但是,这当然只打印出它下面的第二个棒图。我想知道如何让它删除第一个,然后用第二个替换它。

【问题讨论】:

    标签: python


    【解决方案1】:

    在 linux 上你想要os.system('clear')

    在你想要的窗口上os.system('CLS')

    如果你的意思是清除空闲窗口,没有某种插件是不可能的。

    【讨论】:

      【解决方案2】:
      os.system('cls||clear')
      

      清场前睡一两秒会更好​​。

      【讨论】:

      • 欢迎来到 Stack Overflow!请使用tour 并阅读How to Answer。 SO 风格的降价格式帮助是here。请记住,如果未来的人遇到同样的问题,您所写的答案将留在这里供他们阅读。记住这一点,最好总是在你的答案中包含所有相关细节,而不是发布一个简短的、仅代码的答案或包含不合理陈述的答案,例如 “Sleeping 1 or 2清除前几秒...”:为什么会更好?
      【解决方案3】:

      在动态打印输出/显示的情况下,我建议使用 IPython 的display module

      注意:我已经编辑了我的答案(在看到这里的许多回复之后)以允许终端和笔记本显示选择...

      # Create your figures
      fig_1 = """
         _
        |_|
        -|-
         |
        /\\
      """
      
      fig_2 = """
         _
        |_|
        -|/
         |
        /\\
      """
      
      
      # Now the code / display
      from IPython.display import display, clear_output
      import time
      import os
      
      notebook = False
      display(fig_1)
      time.sleep(.1)
      if notebook: clear_output(wait=True)
      else: os.system('clear')
      display(fig_2)     
      

      这样做的更好方法:

      # A fancier way of doing it
      def display_animated_figs(all_figs:list, sleep_s:float, notebook=False):
          """Displays each figure in the list of figs,
          while waiting between to give `animated feel`
      
          notebook: (bool) are you operating in a notebook? (True) Terminal? (False)
          """
          
          for i, fig in enumerate(all_figs):
              # Always clear at start...
              # Allow for notebooks or terminal
              if notebook:
                  clear_output(wait=True)
              else:
                  os.system('clear')
              # After the first figure, wait
              if i>0:
                  time.sleep(sleep_s)
                  
              display(fig)
         
         # All done, nothing to return  
      
      # Now execute
      my_figs = [fig_1, fig_2]
      display_animated_figs(my_figs, 0.1, False)
      

      【讨论】:

        【解决方案4】:

        我有 3 种方法:

        方法一。 打印空行。此代码仅打印 '' 50 次,因此之前打印的任何内容都会消失在屏幕上

        [print('') for x in range(50)]
        

        这是list comprehension,与for loop基本相同。

        方法2。 打印换行符(回车)* 50,这只是 50 个新行

        print('\n' * 50)
        

        (是的,您可以使用 python 对字符串执行 * 和 +。如果您打算使用 python 做更多事情,我强烈建议您研究一下)

        方法 3. cls 命令。这将调用 cls(或清除屏幕)命令。不过你必须导入操作系统。

        os.system('cls')
        

        这使用来自os librarysystem function。这个功能可以做很多有用的事情,一个是清屏

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-16
          • 1970-01-01
          • 2017-03-09
          • 2014-05-21
          • 2012-05-16
          • 2018-11-08
          相关资源
          最近更新 更多