Vitoco.lr
Vitoco.lr

Reputation: 11

How to alter progressbar value in Tkinter Python with timer

So I am trying to run this code:

class PageOne(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        i = 0
        def timeout():
            global i
            barraProgreso["value"] = i
            i += 1
            print("oli")



        # duration is in seconds

        display = tkinter.Label(self, text="Progreso")
        display.grid(row=1, column=1, sticky=tkinter.N)
        barraProgreso = ttk.Progressbar(self, orient='horizontal', length=350, mode='determinate')
        barraProgreso.grid(row=1, column=1, sticky=tkinter.N)

        controller.after(10, timeout())
        #t = Timer(2.0, timeout)
        #t.start()

But this error appears:

local variable 'i' referenced before assignment

I can't get it working and I don't know why, maybe there is some concept about classes and everything they have inside that I am not coding ok, I've been struggling with them for a while as I am kind of new at Python. My goal is to control the value of the progress bar via this timer method.

If you see that I am doing isn't the best way I am opened to all kind of suggestions.

Thanks in advance!

Upvotes: 0

Views: 496

Answers (1)

busybear
busybear

Reputation: 10590

You don't actually want to define i as global in your timeout method; the i defined in __init__ is not global. It lives within the scope of the __init__ method.

A better approach is to define i as an attribute of your class: self.i. Then any method within you class can easily access self.i.

Upvotes: 1

Related Questions