我制作定格动画电影,我想看看我是否可以在编码方面做到这一点。我有这个:
import sleep
print(' _ ')
print(' |_| ')
print(' -|- ')
print(' | ')
print(' / ')
time.sleep(.1)
print(' _ ')
print(' |_| ')
print(' -|/ ')
print(' | ')
print(' / ')
因此,从本质上讲,它看起来就像是棍子在挥舞。但是,这当然只打印出下面的第二个条形图。我想知道如何让它删除第一个,然后用第二个替换它。
在linux上,您需要os.system('clear')
在需要os.system('CLS')
的窗口上
如果你想清除空闲窗口,没有某种插件是不可能的。
os.system('cls||clear')
在清理之前睡1到2秒会更好。
在动态打印输出/显示的情况下,我建议使用IPython的显示模块。
注意:我已经编辑了我的答案(在看到这里的许多回复后(,以允许终端和笔记本电脑显示选项。。。
# 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)
我有三种方法:
方法1。打印空行。这个代码只打印了50次,所以之前打印的任何东西都会从屏幕上消失
[print('') for x in range(50)]
这是一个列表理解,基本上与for循环相同。
方法2。打印换行符(回车(*50,这只是50个换行
print('n' * 50)
(是的,你可以用python在字符串上做*和+。如果你打算用python做更多的事情,我强烈建议你研究一下(
方法3。cls命令。这将调用cls(或clearscreen(命令。不过你必须导入操作系统。
os.system('cls')
这使用了操作系统库中的系统函数。这个功能可以做很多有用的事情,一个是清除屏幕