Python how to make simple animated loading while process is running

前端 未结 6 1783
广开言路
广开言路 2020-12-13 03:30

This is pretty much what I have right now:

import time
import sys

done = \'false\'
#here is the animation
def animate():
    while done == \'false\':
               


        
相关标签:
6条回答
  • 2020-12-13 03:49

    I see this is a threading problem and not just an animated loading problem. Most the answers provided in this QA thread provides only a pseudocode and left the reader on their own.

    Here is an answer I am trying to give using a working example of threading and animated loading.

    The reader may modify in accordance to their needs.

    # import python packages

    import sys, time, threading
    

    # Define your process.

     # Here is an example of the process function:
    
    def the_process_function():
        n = 20
        for i in range(n):
            time.sleep(1)
            sys.stdout.write('\r'+'loading...  process '+str(i)+'/'+str(n)+' '+ '{:.2f}'.format(i/n*100)+'%')
            sys.stdout.flush()
        sys.stdout.write('\r'+'loading... finished               \n')
    

    # Define your animated characters function:

    def animated_loading():
        chars = "/—\|" 
        for char in chars:
            sys.stdout.write('\r'+'loading...'+char)
            time.sleep(.1)
            sys.stdout.flush() 
    

    # define name dan target of your thread

    the_process = threading.Thread(name='process', target=the_process_function)
    

    # start the thread

    the_process.start()
    

    # while the process is alive, call the animated_loading() function

    while the_process.isAlive():
        animated_loading()
    

    The main steps are outlined in the commented out line.

    0 讨论(0)
  • 2020-12-13 03:50

    Use a thread:

    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
    

    I also made a couple of minor modifications to your animate() function, the only really important one was adding sys.stdout.flush() after the sys.stdout.write() calls.

    0 讨论(0)
  • 2020-12-13 03:55

    Here is my code:

    print("Loading:")
    
    #animation = ["10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"]
    animation = ["[■□□□□□□□□□]","[■■□□□□□□□□]", "[■■■□□□□□□□]", "[■■■■□□□□□□]", "[■■■■■□□□□□]", "[■■■■■■□□□□]", "[■■■■■■■□□□]", "[■■■■■■■■□□]", "[■■■■■■■■■□]", "[■■■■■■■■■■]"]
    
    for i in range(len(animation)):
        time.sleep(0.2)
        sys.stdout.write("\r" + animation[i % len(animation)])
        sys.stdout.flush()
    
    print("\n")
    
    0 讨论(0)
  • 2020-12-13 04:05

    Its all done with a few lines of code:

    import time
    import os
    
    anime = ["|", "/", "-", "\\"]
    
    done = False
    
    while done == False:
        for i in anime:
            print('Please wait while system is Loading...', i)
            os.system('clear')
            time.sleep(0.1)
    
    

    Tested and working successfully in the terminal.

    0 讨论(0)
  • 2020-12-13 04:10

    Try this one

    import time
    import sys
    
    
    animation = "|/-\\"
    
    for i in range(100):
        time.sleep(0.1)
        sys.stdout.write("\r" + animation[i % len(animation)])
        sys.stdout.flush()
        #do something
    print("End!")
    
    0 讨论(0)
  • 2020-12-13 04:10
    import sys, time, threading
    
    def your_function_name() :
        # do something here
    
    def loadingAnimation(process) :
        while process.isAlive() :
            chars = "/—\|" 
            for char in chars:
                sys.stdout.write('\r'+'loading '+char)
                time.sleep(.1)
                sys.stdout.flush()
    
    loading_process = threading.Thread(target=your_function_name)
    loading_process.start()
    animated_loading(loading_process)
    loading_process.join()
    
    0 讨论(0)
提交回复
热议问题