我想要打印文字'正在加载......'但它的点会前后移动(在 shell 中)。
我正在创建一个文本游戏,为此它看起来会更好 我知道慢慢写一个字,但点也必须回去。
我在想我应该忘记点回来。为此:
import sys
import time
shell = sys.stdout.shell
shell.write('Loading',"stdout")
str = '........'
for letter in str:
sys.stdout.write(letter)
time.sleep(0.1)
你怎么看?
如果你有那个点会前后移动然后请与我分享。
如果您想了解更多我希望提供给您的信息
感谢
答案 0 :(得分:1)
您可以在STDOUT中使用退格(\b
)进行回溯,以便在再次写入之前返回并“擦除”已写入的字符以模拟动画加载,例如:
import sys
import time
loading = True # a simple var to keep the loading status
loading_speed = 4 # number of characters to print out per second
loading_string = "." * 6 # characters to print out one by one (6 dots in this example)
while loading:
# track both the current character and its index for easier backtracking later
for index, char in enumerate(loading_string):
# you can check your loading status here
# if the loading is done set `loading` to false and break
sys.stdout.write(char) # write the next char to STDOUT
sys.stdout.flush() # flush the output
time.sleep(1.0 / loading_speed) # wait to match our speed
index += 1 # lists are zero indexed, we need to increase by one for the accurate count
# backtrack the written characters, overwrite them with space, backtrack again:
sys.stdout.write("\b" * index + " " * index + "\b" * index)
sys.stdout.flush() # flush the output
请记住,这是一个阻塞过程,因此您必须在for
循环中进行加载检查,或者在单独的线程中运行加载,或者在单独的线程中运行它 - 它将保持只要其本地loading
变量设置为True
,就会以阻止模式运行。
答案 1 :(得分:0)
使用许多功能检查此模块Keyboard。安装它,可能使用以下命令:
pip3 install keyboard
然后在文件 textdot.py 中编写以下代码:
def text(text_to_print,num_of_dots,num_of_loops):
from time import sleep
import keyboard
import sys
shell = sys.stdout.shell
shell.write(text_to_print,'stdout')
dotes = int(num_of_dots) * '.'
for last in range(0,num_of_loops):
for dot in dotes:
keyboard.write('.')
sleep(0.1)
for dot in dotes:
keyboard.write('\x08')
sleep(0.1)
现在将文件粘贴到python文件夹中的 Lib 中 现在您可以使用它,如下例所示:
import textdot
textdot.text('Loading',6,3)
由于
答案 2 :(得分:0)
我相信以下代码是您想要的。只需将其插入您的脚本中,当用户等待时它将闪烁点。
################################################################################
"""
Use this to show progress in the terminal while other processes are runnning
- show_running.py -
"""
################################################################################
#import _thread as thread
import time, sys
def waiting(lenstr=20, zzz=0.5, dispstr='PROCESSING'):
dots = '.' * lenstr
spaces = ' ' * lenstr
print(dispstr.center(lenstr, '*'))
while True:
for i in range(lenstr):
time.sleep(zzz)
outstr = dots[:i] + spaces[i:]
sys.stdout.write('\b' * lenstr + outstr)
sys.stdout.flush()
for i in range(lenstr, 0, -1):
time.sleep(zzz)
outstr = dots[:i] + spaces[i:]
sys.stdout.write('\b' * lenstr + outstr)
sys.stdout.flush()
#------------------------------------------------------------------------------#
if __name__ == '__main__':
import _thread as thread
from tkinter import *
root = Tk()
Label(root, text="I'm Waiting").pack()
start = time.perf_counter()
thread.start_new_thread(waiting, (20, 0.5))
root.mainloop()
finish = time.perf_counter()
print('\nYour process took %.2f seconds to complete.' % (finish - start))
答案 3 :(得分:0)
有点晚了,但对其他人来说并没有那么复杂。
import os, time #import os and time
def loading(): #make a function called loading
spaces = 0 #making a variable to store the amount of spaces between the start and the "."
while True: #infinite loop
print("\b "*spaces+".", end="", flush=True) #we are deleting however many spaces and making them " " then printing "."
spaces = spaces+1 #adding a space after each print
time.sleep(0.2) #waiting 0.2 secconds before proceeding
if (spaces>5): #if there are more than 5 spaces after adding one so meaning 5 spaces (if that makes sense)
print("\b \b"*spaces, end="") #delete the line
spaces = 0 #set the spaces back to 0
loading() #call the function