0

can you please help identify the problem and/or suggest alternative solutions?

pb = ttk.Progressbar(
    root,
    orient='horizontal',
    mode='determinate',
    length=300
)
pb['maximum'] = 300
pb['value'] = 0
pb.grid(row=3, column=2)
pb.start(1)

while True:
    root.update()
    print("+1")
    if pb['maximum'] <= pb['value']-30:
        pb.stop()
        break

i tried to use a loop that stops when the progress bar is full but it just keeps going

1 Answers1

0

It seems like there are two things going on:

First, value is capped at 300 as that is the value set as the maximum. Once it hits 300 it loops back to zero. This guarantess that value - 30 is never greater than maximum.

Second, printing value shows that it increments in both even and odd amounts. This can easily result in value exceeding the 300 cap and resetting before it can be evaluated by the if statement. It ends up being luck of the draw whether value reaches 300 exactly.

Run the code below for a few cycles and it should stop for you. Hope this helps.

import tkinter as tk
from tkinter import ttk
import time

root = tk.Tk()

pb = ttk.Progressbar(
    root,
    orient='horizontal',
    mode='determinate',
    length=300
)
pb['maximum'] = 300
pb['value'] = 0
pb.grid(row=3, column=2)
pb.start(1)

while True:

    # do stuff for a while
    time.sleep(.02)

    pb['value'] += 1
    print(pb['value'])

    if pb['value'] >= pb['maximum']:
        pb.stop()
        break

    root.update()

Edit: Adding the following if statement after the sleep seems to result in completing the loop on the first time every time.

if pb['value'] > 295:
    pb['value'] = 300
Ian McCurdy
  • 125
  • 8