Python yazılımını konsol ekranında çalıştırıyorsanız açılışta veya bir çıktı beklerken kullanıcıya bir animasyon izletebilirsiniz. Bu bölümde birkaç animasyon paylaşıyorum.
Loading animasyonu
#!/usr/bin/env python
from time import sleep
def progress(percent=0, width=30):
hashes = width * percent // 100
blanks = width - hashes
print('\r[', hashes*'#', blanks*' ', ']', f' {percent:.0f}%', sep='',
end='', flush=True)
print('Yükleniyor...')
for i in range(101):
progress(i)
sleep(0.1)
print()
from time import sleep
def progress(percent=0, width=30):
hashes = width * percent // 100
blanks = width - hashes
print('\r[', hashes*'#', blanks*' ', ']', f' {percent:.0f}%', sep='',
end='', flush=True)
print('Yükleniyor...')
for i in range(101):
progress(i)
sleep(0.1)
print()
Dikey Sinüs
import os
import sys
def console_frame(output):
os.system('clear' if os.name == 'posix' else 'CLS')
sys.stdout.write(output + "\n")
sys.stdout.flush()
import time
import math
for t in range(100):
console_frame("\n".join(["*" * (30 + int(30 * math.sin(.1 * x + .1 * t))) for x in range(30)])) # time-varying sine wave
time.sleep(.04)
import sys
def console_frame(output):
os.system('clear' if os.name == 'posix' else 'CLS')
sys.stdout.write(output + "\n")
sys.stdout.flush()
import time
import math
for t in range(100):
console_frame("\n".join(["*" * (30 + int(30 * math.sin(.1 * x + .1 * t))) for x in range(30)])) # time-varying sine wave
time.sleep(.04)
Loading Çizgi Animasyon
import itertools
import threading
import time
import sys
done = False
#here is the animation
def animate():
for c in itertools.cycle(['|', '/', '-', '\\']):
if done:
break
sys.stdout.write('\rloading ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone!')
t = threading.Thread(target=animate)
t.start()
#long process here
time.sleep(10)
done = True
import threading
import time
import sys
done = False
#here is the animation
def animate():
for c in itertools.cycle(['|', '/', '-', '\\']):
if done:
break
sys.stdout.write('\rloading ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone!')
t = threading.Thread(target=animate)
t.start()
#long process here
time.sleep(10)
done = True
Kalın çizgi
import time
def typewriter(text, delay=0.1):
for letter in text:
print(letter, end='', flush=True)
time.sleep(delay)
print()
typewriter("############")
typewriter("############", 0.05)
def typewriter(text, delay=0.1):
for letter in text:
print(letter, end='', flush=True)
time.sleep(delay)
print()
typewriter("############")
typewriter("############", 0.05)