ProgressBar class

mail@pastecode.io avatar
unknown
python
a year ago
1.4 kB
2
Indexable
Never
from tkinter import ttk
import tkinter as tk
import time

class ISTPrgress(ttk.Frame):
    def __init__(self, root) -> None:
        ttk.Frame.__init__(self, root)
        self.root = root
        self.progress = 0
        self.progress_bar = ttk.Progressbar(self.root,
            orient='horizontal',
            mode='determinate',
            length=260)
        self.lbl_percentage = tk.Label(self.root, text="0%")
        self.lbl_mode = ttk.Label(self.root, text="Mode: ")
        self.lbl_step = ttk.Label(self.root, text="starting")
        self.progress_window_layout()

    def progress_window_layout(self):
        self.lbl_mode.grid(row=1, column=0, columnspan=5)
        self.progress_bar.grid(row=2, column=0, columnspan=4)
        self.lbl_percentage.grid(row=2, column=4, columnspan=2)
        self.lbl_step.grid(row=3, column=0, columnspan=5)

    def update_progress_bar(self, progress: int):
        for i in range(1, progress+1):
            self.root.update()
            self.progress += 1
            self.progress_bar['value'] = self.progress
            self.lbl_percentage['text'] = f"{self.progress}%"
            time.sleep(0.1)


    def progress_bar_func(self, progress: int, msg: str):
        self.lbl_step['text'] = msg
        self.update_progress_bar(progress)

# root window
root = tk.Tk()
root.geometry('300x120')
root.title('Progressbar Demo')