0

I wish to know how is the best method to use the same progressbar (determinate and indeterminate). In this example the same progressbar is used for a indeterminate, for a determinate, and function-indeterminate computation. When i run the code only the last progressbar is showed.

from Tkinter import *
import ttk
import tkFileDialog
import time

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

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 = Button(self, text='Start', command=self.start, activeforeground="red")
        self.start.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.pbar_det = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate")
        self.pbar_det.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)

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

    def start(self):
        for i in xrange(10):
            self.pbar_ind.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        for i in xrange(10):
            self.pbar_ind.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        res = foo(10, 2, self)


if __name__=="__main__":
   d = MainWindow()
   d.mainloop()

UPDATE - my not elegant solution is:

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 = Button(self, text='Start', command=self.start, activeforeground="red")
        self.start.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

        self.pbar_f = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate")

        self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate")

        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)



    def start(self):
        for i in xrange(10):
            self.pbar_ind.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        self.pbar_ind.grid_forget()
        self.pbar_det.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)
        for i in xrange(10):
            self.pbar_det.step(1)
            self.update()
            # Busy-wait
            time.sleep(0.1)
        self.pbar_det.grid_forget()
        self.pbar_f.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S)
        res = foo(10, 2, self)
martineau
  • 119,623
  • 25
  • 170
  • 301
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131
  • I used part of your code in answer to question [tkinter progressbar - linked to function?](http://stackoverflow.com/questions/24769798/tkinter-progressbar-linked-to-function) :) – furas Jul 16 '14 at 01:38

1 Answers1

2

If you put all widgets in one cell - they have the same column and row - then you put them one over another. Last one is on the top and you see it.

You can use only one Progressbar widget - for all computation.

You can have few Progressbars and use grid/grid_forget to show one and hide anothers.

You can put Progressbars in different cells and have all on the screen.


EDIT:

example with grid_forget

from Tkinter import *

#-----------------------------------

def on_press():
    global visible

    if visible == 1:
        l1.grid_forget()
        l3.grid(row=0,column=3)
        visible = 3
    else:
        l1.grid(row=0,column=0)
        l3.grid_forget()
        visible = 1

#-----------------------------------

master = Tk()

l1 = Button(master, text='press there >>')
l1.grid(row=0,column=0)

l2 = Button(master, text='change', command=on_press)
l2.grid(row=0,column=1)

l3 = Button(master, text='<< press there')
#l3.grid(row=0,column=3)

visible = 1

master.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Hi @furas. Do you think is better to create a pop progressbar window(s)? or could i update the cell with the first pb, after the second pb, and in the end with the last pb? – Gianni Spear Jul 15 '14 at 17:52
  • 1
    I would use only one `Progressbar` for all computations. – furas Jul 15 '14 at 17:55
  • `grid_forget()` hides widget but always you have to use `grid(...) with arguments as before to show it again. – furas Jul 15 '14 at 17:59
  • 1
    Well, I made example for another question. maybe I can find it. – furas Jul 15 '14 at 18:00
  • See example in answer. – furas Jul 15 '14 at 18:11
  • Thanks @furas, i need to understand how to use your example with progressbar because i am honest it's not very clear – Gianni Spear Jul 15 '14 at 18:31
  • First thing - do you really need only one visible progressbar at any time ? – furas Jul 15 '14 at 18:52
  • My "software" process several steps (quite long because the data is more than 5GB) and for this reason i wish to show a pb for each (long) step. See the update – Gianni Spear Jul 15 '14 at 18:56
  • Thanks @furas, I wish to make a little more now. Is it possible to write inside the progressbar (ex. reading file) to show which step you are processing? – Gianni Spear Jul 15 '14 at 20:54
  • I don't think so. You will have to put text below progressbar. You can create `Frame` with `progressbar` and text below and then you can show/hide that frame. – furas Jul 15 '14 at 21:03
  • i posted a new question. If you have a nice solution i am happy to give you points http://stackoverflow.com/questions/24768090/progressbar-in-tkinter-with-a-label-inside – Gianni Spear Jul 15 '14 at 21:04
  • I see someone already mention about putting widget over widget. – furas Jul 15 '14 at 21:19