打印文本'Loading',在Python shell中向前和向后带有点

krugob8w  于 2023-06-24  发布在  Shell
关注(0)|答案(5)|浏览(164)

我想打印文本'Loading...',但它的点会前后移动(在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)

你觉得呢?
如果你有那个点会来回移动,那么请与我分享。
如果您需要更多的信息,我愿意提供给您。
谢谢

nom7f22z

nom7f22z1#

您可以在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,它将保持在阻塞模式下运行。

wf82jlnq

wf82jlnq2#

检查此模块Keyboard与许多功能。安装它,可能使用以下命令:

pip3 install keyboard

然后在Filetextdot.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)

谢谢

xghobddn

xghobddn3#

有点晚了,但对其他人来说,这并不复杂。

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
s4chpxco

s4chpxco4#

我相信下面的代码就是你要找的。只需在脚本中执行此操作,在用户等待时,它将 Flink 圆点。

################################################################################
"""
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))
ogsagwnx

ogsagwnx5#

我也遇到过类似的问题。我想了很多来解决我的问题。代码应该用圆点打印“正在加载”。

import sys
import time

def print_loading_dots():
    while True:
        for i in range(4):
            sys.stdout.write('\r' + 'Loading' + '.' * i)
            sys.stdout.flush()
            time.sleep(0.5)
        sys.stdout.write("\033[K")
        sys.stdout.write("\r" + "Loading   ")

print_loading_dots()

相关问题