Preface
Progressbar is a widget in the Tkinter library for creating and managing progress bars. It displays the progress of tasks in a graphical user interface and provides a variety of styling and configuration options.
Using Progressbar, you can display the status of tasks according to fixed or uncertain progress. It can display the percentage of task completion or, in the case of uncertainty, display an animation to indicate that the task is in progress.
The following are some important properties and methods of Progressbar:
- length: Specifies the length of the progress bar.
- mode: Specifies the mode of the progress bar, which can be "determinate" (determinate mode) or "indeterminate" (indeterminate mode).
- maximum: Set the maximum value of the progress bar, the default is 100.
- value: Set the current value of the progress bar.
- start(): Starts the animation effect of the progress bar, only valid in uncertain mode.
- stop(): Stops the animation effect of the progress bar, only valid in indeterminate mode.
By using these properties and methods, you can create a custom progress bar and update and control it as needed.
1. indeterminate mode
In this mode, the pointer moves left and right. The main purpose is to let the user know that the program is still running.
import tkinter as tk
from tkinter.ttk import Progressbar
class Simulate_Waiting_State:
def __init__(self):
self.root = tk.Tk()
self.root.title('进度条下载演示')
self.root.geometry("300x150+1100+150")
self.interface()
def interface(self):
# 创建进度条
self.progress_bar = Progressbar(self.root, length=200, mode="indeterminate")
# 创建按钮
self.start_button = tk.Button(self.root, text="开始", command=self.start_progress)
self.stop_button = tk.Button(self.root, text="停止", command=self.stop_progress)
# 将进度条和按钮放置在窗口中
self.progress_bar.grid(row=0, column=1, pady=20, padx=50, columnspan=100)
self.start_button.grid(row=1, column=1, padx=75)
self.stop_button.grid(row=1, column=3)
def start_progress(self):
self.progress_bar.start()
def stop_progress(self):
self.progress_bar.stop()
if __name__ == '__main__':
run = Simulate_Waiting_State()
run.root.mainloop()
2. determine mode
1. Simulate download progress
import tkinter as tk
from tkinter.ttk import Progressbar
import threading
import time
class Download_Files:
def __init__(self):
self.root = tk.Tk()
self.root.title('进度条下载演示')
self.root.geometry("300x150+1100+150")
self.interface()
def interface(self):
# 创建进度条
self.progress_bar = Progressbar(self.root, length=200, mode="determinate")
# 创建按钮
self.start_button = tk.Button(self.root, text="开始下载", command=self.download)
# 将进度条和按钮放置在窗口中
self.progress_bar.grid(row=0, pady=20, padx=50)
self.start_button.grid(row=1, padx=50)
# 进度值最大值
self.progress_bar['maximum'] = 100
def download(self):
"""进度条模拟显示下载进度"""
# 进度值初始值
initial_value = 0
while initial_value < 100:
initial_value += 1
# 更新进度条的值
self.progress_bar['value'] = initial_value
self.root.update()
# 模拟等待时间时间
time.sleep(0.1)
def thread_management(self):
"""启用子线程下载文件"""
T1 = threading.Thread(target=self.download, daemon=True)
T1.start()
if __name__ == '__main__':
run = Download_Files()
run.root.mainloop()
2. Real download progress
import tkinter as tk
from tkinter.ttk import Progressbar
from tkinter import messagebox
import threading
import requests
class Download_Files:
def __init__(self):
self.root = tk.Tk()
self.root.title('进度条下载演示')
self.root.geometry("300x150+850+350")
self.interface()
def interface(self):
# 创建进度条
self.progress_bar = Progressbar(self.root, length=200, mode="determinate")
# 创建按钮
self.start_button = tk.Button(self.root, text="开始下载", command=self.thread_group)
# 将进度条和按钮放置在窗口中
self.progress_bar.grid(row=0, pady=20, padx=50)
self.start_button.grid(row=1, padx=50)
def download(self):
# 禁用按钮
self.start_button.config(state=tk.DISABLED)
# 下载地址
url = 'http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2'
file_data = requests.get(url, stream=True)
# 获取文件大小,单位字节(B)
if "content-length" in file_data.headers:
maxbyte = int(file_data.headers["content-length"])
# 截取文件名称
filename = url.split("/")[-1]
with open(filename, "wb") as f:
downloaded_bytes = 0
for chunk in file_data.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
downloaded_bytes += len(chunk)
# 更新进度条的值
# 将已下载的字节数除以文件总大小(maxbyte),然后乘以100,得到已下载的数据量相对于文件总大小的百分比
self.progress_bar['value'] = downloaded_bytes / maxbyte * 100
self.root.update()
# 弹窗提示下载完成
messagebox.showinfo("下载提示", "文件下载完成!")
# 恢复按钮的可点击状态
self.start_button.config(state=tk.NORMAL)
# 下载完成后重置进度条的值
self.progress_bar['value'] = 0
def thread_group(self):
"""启用子线程下载"""
T1 = threading.Thread(name='download', target=self.download, daemon=True)
T1.start()
if __name__ == '__main__':
run = Download_Files()
run.root.mainloop()