import queue
import tkinter as tk
from tkinter import ttk


class StatusUi:

    def __init__(self, frame, q):
        self.q = q
        self.frame = frame
        self.pb_max = 100
        # ttk.Label(self.frame, text='This is just an example of a third frame').grid(
        #     column=0, row=1, sticky=W)
        self.progress_bar = ttk.Progressbar(
            self.frame, mode='determinate')
        self.progress_bar.pack(expand=True, fill=tk.X, side=tk.TOP)

        self.frame.after(100, self.check_q)

    def check_q(self):
        # Check every 100ms if there is a new message in the queue to display
        while True:
            try:
                record = self.q.get(block=False)
            except queue.Empty:
                break
            else:
                if record:
                    step = 1
                    self.progress_bar.step(step)
                    if step >= self.pb_max:
                        step = 1
                    else:
                        step += 1
                else:
                    self.progress_bar.config(value=0)
        self.frame.after(100, self.check_q)
