📜  Tkinter 中的进度条小部件 | Python

📅  最后修改于: 2022-05-13 01:55:05.698000             🧑  作者: Mango

Tkinter 中的进度条小部件 | Python

这个小部件的目的是向用户保证正在发生的事情。它可以在以下两种模式之一中运行——
确定模式下,小部件显示在程序控制下从头到尾移动的指示器。
不确定模式下,小部件是动画的,因此用户会相信某些事情正在进行中。在这种模式下,指示器在小部件的两端之间来回反弹。

句法:

widget_object = Progressbar(parent, **options)


代码 #1确定模式下

# importing tkinter module
from tkinter import * from tkinter.ttk import *
  
# creating tkinter window
root = Tk()
  
# Progress bar widget
progress = Progressbar(root, orient = HORIZONTAL,
              length = 100, mode = 'determinate')
  
# Function responsible for the updation
# of the progress bar value
def bar():
    import time
    progress['value'] = 20
    root.update_idletasks()
    time.sleep(1)
  
    progress['value'] = 40
    root.update_idletasks()
    time.sleep(1)
  
    progress['value'] = 50
    root.update_idletasks()
    time.sleep(1)
  
    progress['value'] = 60
    root.update_idletasks()
    time.sleep(1)
  
    progress['value'] = 80
    root.update_idletasks()
    time.sleep(1)
    progress['value'] = 100
  
progress.pack(pady = 10)
  
# This button will initialize
# the progress bar
Button(root, text = 'Start', command = bar).pack(pady = 10)
  
# infinite loop
mainloop()

输出:

代码 #2:处于不确定模式
# importing tkinter module
from tkinter import * from tkinter.ttk import *
  
# creating tkinter window
root = Tk()
  
# Progress bar widget
progress = Progressbar(root, orient = HORIZONTAL,
            length = 100, mode = 'indeterminate')
  
# Function responsible for the updation
# of the progress bar value
def bar():
    import time
    progress['value'] = 20
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 40
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 50
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 60
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 80
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 100
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 80
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 60
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 50
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 40
    root.update_idletasks()
    time.sleep(0.5)
  
    progress['value'] = 20
    root.update_idletasks()
    time.sleep(0.5)
    progress['value'] = 0
      
  
progress.pack(pady = 10)
  
# This button will initialize
# the progress bar
Button(root, text = 'Start', command = bar).pack(pady = 10)
  
# infinite loop
mainloop()

输出: