您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python進(jìn)度條的制作代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
import sys,time #導(dǎo)入模塊 for i in range(50): #進(jìn)度條的長(zhǎng)度 sys.stdout.write("#") #進(jìn)度條的內(nèi)容,這里要注意了,pycharm有可能不顯示write的方法 sys.stdout.flush() #刷新緩存 time.sleep(0.5) #間隔時(shí)間,和shell的sleep差不多吧
或者
import sys class progressbar(object): def __init__(self, finalcount, block_char='.'): self.finalcount = finalcount self.blockcount = 0 self.block = block_char self.f = sys.stdout if not self.finalcount: return self.f.write('\n------------------ % Progress -------------------1\n') self.f.write(' 1 2 3 4 5 6 7 8 9 0\n') self.f.write('----0----0----0----0----0----0----0----0----0----0\n') def progress(self, count): count = min(count, self.finalcount) if self.finalcount: percentcomplete = int(round(100.0 * count / self.finalcount)) if percentcomplete < 1: percentcomplete = 1 else: percentcomplete = 100 blockcount = int(percentcomplete // 2) if blockcount <= self.blockcount: return for i in range(self.blockcount, blockcount): self.f.write(self.block) self.f.flush() self.blockcount = blockcount if percentcomplete == 100: self.f.write("\n") if __name__ == "__main__": from time import sleep pb = progressbar(8, "*") for count in range(1, 9): pb.progress(count) sleep(0.2) pb = progressbar(100) pb.progress(20) sleep(0.3) pb.progress(47) sleep(0.3) pb.progress(90) sleep(0.3) pb.progress(100) print "testing 1:" pb = progressbar(1) pb.progress(1)
或者
# -*- coding: UTF-8 -*- import sys, time class ShowProcess(): """ 顯示處理進(jìn)度的類 調(diào)用該類相關(guān)函數(shù)即可實(shí)現(xiàn)處理進(jìn)度的顯示 """ i = 0 # 當(dāng)前的處理進(jìn)度 max_steps = 0 # 總共需要處理的次數(shù) max_arrow = 50 #進(jìn)度條的長(zhǎng)度 # 初始化函數(shù),需要知道總共的處理次數(shù) def __init__(self, max_steps): self.max_steps = max_steps self.i = 0 # 顯示函數(shù),根據(jù)當(dāng)前的處理進(jìn)度i顯示進(jìn)度 # 效果為[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00% def show_process(self, i=None): if i is not None: self.i = i else: self.i += 1 num_arrow = int(self.i * self.max_arrow / self.max_steps) #計(jì)算顯示多少個(gè)'>' num_line = self.max_arrow - num_arrow #計(jì)算顯示多少個(gè)'-' percent = self.i * 100.0 / self.max_steps #計(jì)算完成進(jìn)度,格式為xx.xx% process_bar = '[' + '>' * num_arrow + '-' * num_line + ']'\ + '%.2f' % percent + '%' + '\r' #帶輸出的字符串,'\r'表示不換行回到最左邊 sys.stdout.write(process_bar) #這兩句打印字符到終端 sys.stdout.flush() def close(self, words='done'): print '' print words self.i = 0 if __name__=='__main__': max_steps = 100 process_bar = ShowProcess(max_steps) for i in range(max_steps + 1): process_bar.show_process() time.sleep(0.05) process_bar.close()
或者
from Tkinter import * def resize(ev=one): label.config(font='Helvetica -%d bold' % scale.get()) top = Tk() top.geometry() label = Label(top, text = 'hello world!', font = 'Helvetica -12 bold') label.pack(fill=Y,expand=1) scale = Scale(top, from_=10, to=40, orient=HORIZONTAL, command=resize) scale.set(12) scale.pack(fill=X, expand=1) quit = Button(top, text="QUIT", command=top.quit, activeforeground='white', activebackground='red') quit.pack() mainloop()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。