1

I am working on a progress bar in Tkinter using object-oriented design, and I'm having an issue.

from tkinter import *
from tkinter import ttk

class Status:
    def __init__(self):
        self.root = Tk()
        self.root.geometry("400x20")
        self.loading = ttk.Progressbar(self.root, length=15, value=0, orient=HORIZONTAL, command=self.start_progress())
        self.loading.pack(fill=X)
        self.root.mainloop()

    def start_progress(self):
        self.loading.start(10)

bar = Status()

I'm supposed to get a progress bar which indefinitely loads, but instead, I'm getting

"self.loading.start(10)
AttributeError: 'Status' object has no attribute 'loading'". 

What I want is for the progress bar to automatically update without the use of any button. it should fill up and stop when it is full.

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
Dre Day
  • 25
  • 8
  • At a first look you code seem's correct for me, could you please show more with the part that is used to call and use the Status class ? – Xiidref Jul 02 '19 at 15:59
  • I just edited it to show just that – Dre Day Jul 02 '19 at 16:06
  • 1
    Lose the `()` from the command. it needs to be `command=self.start_progress`, not `command=self.start_progress()` – Novel Jul 02 '19 at 16:09
  • Did that and now it says ```_tkinter.TclError: unknown option "-command"``` – Dre Day Jul 02 '19 at 16:12
  • 1
    I don't think progress bar accepts a command callback. What are you trying to achieve by passing command? – Henry Yik Jul 02 '19 at 16:30
  • What I want is for the progress bar to automatically update without the use of any button. it should fill up and stop when it is full. If there is another way, i would be more welcome to it – Dre Day Jul 02 '19 at 16:42

1 Answers1

0

Normally you would create a IntVar and set it as a variable for your Progressbar. Then you can trace the changes on your IntVar and stop when necessary.

from tkinter import *
from tkinter import ttk

class Status:
    def __init__(self):
        self.root = Tk()
        self.root.geometry("400x20")
        self.var = IntVar()
        self.loading = ttk.Progressbar(self.root, length=15, variable=self.var, orient=HORIZONTAL)
        self.loading.pack(fill=X)
        self.var.trace("w",self.trace_method)
        self.start_progress()
        self.root.mainloop()

    def start_progress(self):
        self.loading.start(10)

    def trace_method(self,*args):
        if self.var.get() >= 99: #when it reaches 100 it would go back to 0
            self.loading.stop()

bar = Status()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40