欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Tkinter使用Progressbar創(chuàng)建和管理進度條的操作代碼

 更新時間:2023年07月12日 16:37:39   作者:檬檸wan  
Progressbar是Tkinter庫中的一個小部件,用于創(chuàng)建和管理進度條,這篇文章主要介紹了Tkinter使用Progressbar創(chuàng)建和管理進度條,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

前言

Progressbar是Tkinter庫中的一個小部件,用于創(chuàng)建和管理進度條。它可以在圖形用戶界面中顯示任務(wù)的進度,并提供了多種樣式和配置選項。

使用Progressbar,你可以按照固定或不確定的進度展示任務(wù)的進行狀態(tài)。它可以顯示任務(wù)完成的百分比,或者在不確定的情況下,顯示一個動畫效果表示任務(wù)正在進行。

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

  • length:指定進度條的長度。
  • mode:指定進度條的模式,可以是"determinate"(確定模式)或"indeterminate"(不確定模式)。
  • maximum:設(shè)置進度條的最大值,默認為100。
  • value:設(shè)置進度條的當前值。
  • start():啟動進度條的動畫效果,僅在不確定模式下有效。
  • stop():停止進度條的動畫效果,僅在不確定模式下有效。

通過使用這些屬性和方法,你可以創(chuàng)建一個自定義的進度條并根據(jù)需要進行更新和控制。

一、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):
        # 創(chuàng)建進度條
        self.progress_bar = Progressbar(self.root, length=200, mode="indeterminate")
        # 創(chuàng)建按鈕
        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):
        # 創(chuàng)建進度條
        self.progress_bar = Progressbar(self.root, length=200, mode="indeterminate")
        # 創(chuàng)建按鈕
        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()

請?zhí)砑訄D片描述

二、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):
        # 創(chuàng)建進度條
        self.progress_bar = Progressbar(self.root, length=200, mode="determinate")
        # 創(chuàng)建按鈕
        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):
        # 創(chuàng)建進度條
        self.progress_bar = Progressbar(self.root, length=200, mode="determinate")
        # 創(chuàng)建按鈕
        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)
        # 獲取文件大小,單位字節(jié)(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)
                    # 更新進度條的值
                    # 將已下載的字節(jié)數(shù)除以文件總大?。╩axbyte),然后乘以100,得到已下載的數(shù)據(jù)量相對于文件總大小的百分比
                    self.progress_bar['value'] = downloaded_bytes / maxbyte * 100
                    self.root.update()
        # 彈窗提示下載完成
        messagebox.showinfo("下載提示", "文件下載完成!")
        # 恢復(fù)按鈕的可點擊狀態(tài)
        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()

請?zhí)砑訄D片描述

到此這篇關(guān)于Tkinter使用Progressbar創(chuàng)建和管理進度條的文章就介紹到這了,更多相關(guān)Tkinter Progressbar進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 跟老齊學(xué)Python之賦值,簡單也不簡單

    跟老齊學(xué)Python之賦值,簡單也不簡單

    在《初識永遠強大的函數(shù)》一文中,有一節(jié)專門討論“取名字的學(xué)問”,就是有關(guān)變量名稱的問題,本溫故而知新的原則,這里要復(fù)習(xí)一下
    2014-09-09
  • Pycharm 2019 破解激活方法圖文詳解

    Pycharm 2019 破解激活方法圖文詳解

    這篇文章主要介紹了Pycharm 2019 破解激活方法圖文詳解,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • Python中順序表的實現(xiàn)簡單代碼分享

    Python中順序表的實現(xiàn)簡單代碼分享

    這篇文章主要介紹了Python中順序表的實現(xiàn)簡單代碼分享,展示了代碼運行結(jié)果,然后分享了相關(guān)實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python數(shù)據(jù)分析之Python和Selenium爬取BOSS直聘崗位

    Python數(shù)據(jù)分析之Python和Selenium爬取BOSS直聘崗位

    今天教各位小伙伴怎么用Python和Selenium爬取BOSS直聘崗位,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python爬蟲和數(shù)據(jù)分析的小伙伴有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • 完美解決Django2.0中models下的ForeignKey()問題

    完美解決Django2.0中models下的ForeignKey()問題

    這篇文章主要介紹了完美解決Django2.0中models下的ForeignKey()問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 利用python開發(fā)app實戰(zhàn)的方法

    利用python開發(fā)app實戰(zhàn)的方法

    這篇文章主要介紹了利用python開發(fā)app實戰(zhàn)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 一篇文章從零開始創(chuàng)建conda環(huán)境、常用命令的使用及pycharm配置項目環(huán)境

    一篇文章從零開始創(chuàng)建conda環(huán)境、常用命令的使用及pycharm配置項目環(huán)境

    在Conda中創(chuàng)建新環(huán)境是一個非常有用的做法,尤其是當你需要為不同的項目安裝不同版本的軟件包時,這篇文章主要給大家介紹了關(guān)于從零開始創(chuàng)建conda環(huán)境、常用命令的使用及pycharm配置項目環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • Python報錯:TypeError:?‘xxx‘?object?is?not?subscriptable解決辦法

    Python報錯:TypeError:?‘xxx‘?object?is?not?subscriptable解決

    這篇文章主要給大家介紹了關(guān)于Python報錯:TypeError:?‘xxx‘?object?is?not?subscriptable的解決辦法,TypeError是Python中的一種錯誤,表示操作或函數(shù)應(yīng)用于不合適類型的對象時發(fā)生,文中將解決辦法介紹的非常詳細,需要的朋友可以參考下
    2024-08-08
  • python驗證碼識別教程之利用滴水算法分割圖片

    python驗證碼識別教程之利用滴水算法分割圖片

    這篇文章主要給大家介紹了關(guān)于python驗證碼識別教程之利用滴水算法分割圖片的相關(guān)資料,文章中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • python 判斷參數(shù)為Nonetype類型或空的實例

    python 判斷參數(shù)為Nonetype類型或空的實例

    今天小編就為大家分享一篇python 判斷參數(shù)為Nonetype類型或空的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10

最新評論