Tkinter使用Progressbar创建和管理进度条的操作代码

 更新时间:2023年07月12日 16:37:39   作者:檬柠wan  
Progressbar是Tkinter库中的一个小部件,用于创建和管理进度条,这篇文章主要介绍了Tkinter使用Progressbar创建和管理进度条,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

前言

Progressbar是Tkinter库中的一个小部件,用于创建和管理进度条。它可以在图形用户界面中显示任务的进度,并提供了多种样式和配置选项。

使用Progressbar,你可以按照固定或不确定的进度展示任务的进行状态。它可以显示任务完成的百分比,或者在不确定的情况下,显示一个动画效果表示任务正在进行。

以下是一些Progressbar的重要属性和方法:

  • length:指定进度条的长度。
  • mode:指定进度条的模式,可以是"determinate"(确定模式)或"indeterminate"(不确定模式)。
  • maximum:设置进度条的最大值,默认为100。
  • value:设置进度条的当前值。
  • start():启动进度条的动画效果,仅在不确定模式下有效。
  • stop():停止进度条的动画效果,仅在不确定模式下有效。

通过使用这些属性和方法,你可以创建一个自定义的进度条并根据需要进行更新和控制。

一、indeterminate 模式

在这个模式下指针左右移动,主要目的是要让用户知道程序还在运行

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()
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()

请添加图片描述

二、determinate模式

1、模拟下载进度

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、真实下载进度

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()

请添加图片描述

到此这篇关于Tkinter使用Progressbar创建和管理进度条的文章就介绍到这了,更多相关Tkinter Progressbar进度条内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python爬虫之遍历单个域名

    python爬虫之遍历单个域名

    在本篇文章里小编给大家整理的是一篇关于python遍历单个域名的知识点和操作方法,有需要的朋友们学习下。
    2019-11-11
  • 深入理解最新Python中的Match Case

    深入理解最新Python中的Match Case

    最近发布的 Python 3.10 的所有主要新特性中最重要就是 Match-Case 语法,本文将带你深入探讨,会发现 Match-Case 其实没有那么简单
    2021-11-11
  • 18个好用的Python技巧分享(建议收藏)

    18个好用的Python技巧分享(建议收藏)

    在这篇文章中,我们将讨论最常用的一些python技巧,这些技巧都是在日常工作中使用过的简单的Trick,小编觉得好东西就是要拿出来和大家一起分享哒
    2023-07-07
  • Python中的enumerate() 函数用法详解

    Python中的enumerate() 函数用法详解

    enumerate()是python的内置函数,将一个可遍历iterable数据对象(如list列表、tuple元组或str字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中,这篇文章主要介绍了Python中的enumerate() 函数用法详解,需要的朋友可以参考下
    2024-01-01
  • Python实现string字符串连接的方法总结【8种方式】

    Python实现string字符串连接的方法总结【8种方式】

    这篇文章主要介绍了Python实现string字符串连接的方法,结合实例形式总结分析了Python实现字符串连接的8种常见操作技巧,需要的朋友可以参考下
    2018-07-07
  • Python Django ORM连表正反操作技巧

    Python Django ORM连表正反操作技巧

    这篇文章主要介绍了Django-ORM-连表正反操作,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-06-06
  • pandas中Series的使用方式

    pandas中Series的使用方式

    这篇文章主要介绍了pandas中Series的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • python实现红包裂变算法

    python实现红包裂变算法

    这篇文章主要介绍了python实现红包裂变算法的相关资料,需要的朋友可以参考下
    2016-02-02
  • python字符串运算符详情

    python字符串运算符详情

    这篇文章主要介绍了 python字符串运算符详情,在编程里,用的最多的就是字符串,字符串同时也是各类数据的中转站,下文基于python的相关资料介绍字符串运算符,需要的小伙伴可以参考一下
    2022-04-04
  • Python xlwt模块使用代码实例

    Python xlwt模块使用代码实例

    这篇文章主要介绍了Python xlwt模块使用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论