Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้าง Download Progress Bar ใน Tkinter


ภาพรวม

สมมติสร้างแอปโต้ตอบกับ Sources และ Files เช่น Download Files, Tracking File เพื่อทำ Progressbar ใช้ Package tkinter.ttk ที่มี Module Progressbar

สร้าง Object ของ Progressbar Orientation Horizontal นิยาม Function เพิ่มค่า Progressbar และ Update ตลอด

ตัวอย่างโค้ด

# Import the required libraries
from tkinter import *
from tkinter.ttk import *
import time
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of frame
win.geometry("620x400")
# Define a function
def start():
    task = 10
    x = 0
    while(x < task):
        time.sleep(1)
        bar['value'] += 10
        x += 1
        win.update_idletasks()
bar = Progressbar(win, orient=HORIZONTAL, length=300)
bar.pack(pady=20)
# Create a button
Button(win, text="Download", command=start).pack(pady=20)
win.mainloop()

ผลลัพธ์

รันโค้ดจะแสดง Download Bar คลิก "Download" จะเสร็จอัตโนมัติ

วิธีสร้าง Download Progress Bar ใน Tkinter