SoFunction
Updated on 2024-11-14

Python progress bar making code example

This article introduces the production of Python progress bar code examples, the text of the sample code through the introduction of the very detailed, for everyone to learn or work with certain reference to the learning value, you can refer to the next!

import sys,time
#Import Module
for i in range(50):
# Length of the progress bar
  ("#")
The contents of the # progress bar. Be careful here, pycharm may not show the write method.
  ()
# Refresh the cache
  (0.5)
#interval time,cap (a poem)shell(used form a nominal expression)sleepSomething like that.

or

import sys
 
class progressbar(object):
 
  def __init__(self, finalcount, block_char='.'):
     = finalcount
     = 0
     = block_char
     = 
    if not :
      return
    ('\n------------------ % Progress -------------------1\n')
    (' 1 2 3 4 5 6 7 8 9 0\n')
    ('----0----0----0----0----0----0----0----0----0----0\n')
 
  def progress(self, count):
    count = min(count, )
    if :
      percentcomplete = int(round(100.0 * count / ))
      if percentcomplete < 1:
        percentcomplete = 1
    else:
      percentcomplete = 100
    blockcount = int(percentcomplete // 2)
    if blockcount <= :
      return
    for i in range(, blockcount):
      ()
    ()
     = blockcount
    if percentcomplete == 100:
      ("\n")
 
if __name__ == "__main__":
  from time import sleep
  pb = progressbar(8, "*")
  for count in range(1, 9):
    (count)
    sleep(0.2)
  pb = progressbar(100)
  (20)
  sleep(0.3)
  (47)
  sleep(0.3)
  (90)
  sleep(0.3)
  (100)
  print "testing 1:"
  pb = progressbar(1)
  (1)

or

# -*- coding: UTF-8 -*-
import sys, time
class ShowProcess():
  """
  Classes for displaying processing progress
  The display of processing progress can be achieved by calling the relevant functions of this class
  """
  i = 0 # Current processing progress
  max_steps = 0 # of times total processing is required
  max_arrow = 50 # Length of the progress bar

  # Initialize the function, need to know the total number of times it was processed
  def __init__(self, max_steps):
    self.max_steps = max_steps
     = 0

  # Display function that displays progress according to the current processing progress i
  # The effect is [>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> gt;>>>>>>>>>>>>>>>>>]100.00%
  def show_process(self, i=None):
    if i is not None:
       = i
    else:
       += 1
    num_arrow = int( * self.max_arrow / self.max_steps) #calculate how many '>' are displayed
    num_line = self.max_arrow - num_arrow #Calculate how many '-' are displayed
    percent =  * 100.0 / self.max_steps # Calculate progress towards completion in % format
    process_bar = '[' + '>' * num_arrow + '-' * num_line + ']'\
           + '%.2f' % percent + '%' + '\r' #String with output, '\r' means no line feed back to the leftmost side
    (process_bar) # These two sentences print characters to the terminal
    ()
  def close(self, words='done'):
    print ''
    print words
     = 0
if __name__=='__main__':
  max_steps = 100
  process_bar = ShowProcess(max_steps)
  for i in range(max_steps + 1):
    process_bar.show_process()
    (0.05)
  process_bar.close()

or

from Tkinter import *
def resize(ev=one):
  (font='Helvetica -%d bold' % ())

top = Tk()
()

label = Label(top, text = 'hello world!', font = 'Helvetica -12 bold')
(fill=Y,expand=1)

scale = Scale(top, from_=10, to=40, orient=HORIZONTAL, command=resize)
(12)
(fill=X, expand=1)
quit = Button(top, text="QUIT", command=, activeforeground='white', activebackground='red')
()

mainloop()

This is the whole content of this article.