0

I wish to understand how to embed a progessbar of Tkinter in a function (ex: foo) with an easy example. I wrote this code where the first button is a indeterminate progessbar, the second is a determinate progessbar, and the third button the function where i try to insert the progessbar. I tried with a error message to insert self.pbar_ind.step(1) and self.update() inside foo

**res = foo(self, 5)
NameError: global name 'foo' is not defined**

.

from Tkinter import *
import ttk
import tkFileDialog
import time

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("ProgressBar example")
        self.master.minsize(200, 100)
        self.grid(sticky=E+W+N+S)

        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        self.start_ind = Button(self, text='Start indeterminate', command=self.start_ind, activeforeground="red")
        self.start_ind.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_ind = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate")
        self.pbar_ind.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.start_det = Button(self, text='Start determinate', command=self.start_det, activeforeground="red")
        self.start_det.grid(row=2, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate")
        self.pbar_det.grid(row=3, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.inside_f = Button(self, text='Start function', command=self.start_fun, activeforeground="red")
        self.inside_f.grid(row=4, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_f = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate")
        self.pbar_f.grid(row=5, column=0, pady=2, padx=2, sticky=E+W+N+S)

    def foo(self, m):
        for i in xrange(m):
            i * 2
            self.pbar_det.step(1)
            self.update()
            time.sleep(0.1)
        return i


    def start_ind(self):
        for i in xrange(500):
            self.pbar_ind.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)

    def start_det(self):
        for i in xrange(500):
            self.pbar_det.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)

    def start_fun(self):
        res = foo(500)


if __name__=="__main__":
   d = MainWindow()
   d.mainloop()
12
  • Where is the (full) error message ? Commented Jul 15, 2014 at 4:15
  • Why foo() is outside class ? Commented Jul 15, 2014 at 4:18
  • Hi Furas, I cannot write self.pbar_ind.step(1) and self.update() outside the class Commented Jul 15, 2014 at 4:19
  • So why foo() is outside class - it should be part of class. Commented Jul 15, 2014 at 4:19
  • 1
    I add version without class. Commented Jul 15, 2014 at 4:41

1 Answer 1

1

You can do two things

1) add foo() to class:

def start_fun(self):
    res = self.foo(500) # use self.

def foo(self, m):
    for i in xrange(m):
        i * 2
        self.pbar_det.step(1)
        self.update()
        time.sleep(0.1)
    return i

2) send self to external foo()

def foo(m, self_from_class):
    for i in xrange(m):
        i * 2
        self_from_class.pbar_det.step(1)
        self_from_class.update()
        time.sleep(0.1)
    return i

class MainWindow(Frame):

    # ...

    def start_fun(self):
        res = foo(500, self)

BTW:

everything without class

from Tkinter import *
import time

def foo(m):
    for i in xrange(m):
        i * 2
        pbar_f.step(1)
        master.update()
        time.sleep(0.1)
    return i

def start_ind():
    for i in xrange(500):
        pbar_ind.step(1)
        master.update()
        # Busy-wait
        time.sleep(0.1)

def start_det():
    for i in xrange(500):
        pbar_det.step(1)
        master.update()
        # Busy-wait
        time.sleep(0.1)

def start_fun(self):
    res = foo(500)
    print 'res:', res

master = Tk()

master.title("ProgressBar example")
master.minsize(200, 100)
#grid(sticky=E+W+N+S)

#top = winfo_toplevel()
master.rowconfigure(0, weight=1)
master.columnconfigure(0, weight=1)

start_ind = Button(master, text='Start indeterminate', command=start_ind, activeforeground="red")
start_ind.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

pbar_ind = ttk.Progressbar(master, orient="horizontal", length=300, mode="indeterminate")
pbar_ind.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)

start_det = Button(master, text='Start determinate', command=start_det, activeforeground="red")
start_det.grid(row=2, column=0, pady=2, padx=2, sticky=E+W+N+S)

pbar_det = ttk.Progressbar(master, orient="horizontal", length=300, mode="determinate")
pbar_det.grid(row=3, column=0, pady=2, padx=2, sticky=E+W+N+S)

inside_f = Button(master, text='Start function', command=start_fun, activeforeground="red")
inside_f.grid(row=4, column=0, pady=2, padx=2, sticky=E+W+N+S)

pbar_f = ttk.Progressbar(master, orient="horizontal", length=300, mode="determinate")
pbar_f.grid(row=5, column=0, pady=2, padx=2, sticky=E+W+N+S)

master.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.