2
import customtkinter as CTK

class App(CTK.CTk):
    def __init__(self):
        super().__init__()

    self.bar = CTK.CTkProgressBar(master=self.checkbox, orientation='horizontal', mode='determinate')
    self.bar.grid(row=10, column=0, pady=10, padx=20, sticky="n")


    def test(self):
    for x in range(500):
        return x**2

I want so that when I want I run the test function (via a button I already have made which works fine by itself) the bar starts and when it ends it stops. Although best would be if there was a way to use tqdm progress bar in the UI itself in place of the tkinter one?

I've tried adding self.bar.start() and stop at start/end of the function but doesn't seem to work. It only runs after the function itself is done.

sofquestions
  • 21
  • 1
  • 2

1 Answers1

2

Apart from starting and stopping the progress bar, you need to update it on each iteration. The documentation states that the bar goes from 0 to 1, so you will need to come up with a way to measure your increments and then re-scale them to a float between the required range.

You will also need to set an initial value for the progress bar immediately after your widget declaration, since the default is 0.5 (half-way).

Additionally, you will need to use the update_idletasks() method for the progress bar to actually move on each step. Otherwise, it will wait until the task is executed and then update the values.

import customtkinter as CTK

class App(CTK.CTk):
    def __init__(self):
        super().__init__()

    self.bar = CTK.CTkProgressBar(master=self.checkbox,
                                  orientation='horizontal',
                                  mode='determinate')
    
    self.bar.grid(row=10, column=0, pady=10, padx=20, sticky="n")
    
    # Set default starting point to 0
    self.bar.set(0)
    
    def test(self):
        n = 500
        iter_step = 1/n
        progress_step = iter_step
        self.bar.start()
        
        for x in range(500):
            self.bar.set(progress_step)
            progress_step += iter_step
            self.update_idletasks()
        self.bar.stop()

Now you just need to include a button or some other object to initialize your function.

Regarding the return statement, I would not recommend doing it that way. You could alternatively append each element to a list and return it upon progress bar conclusion.

Hope this helps.

pabloagn
  • 43
  • 7