I am working on the Tkinter.......i need progress bar in determinate mode........

can some one please give simple example of it, i m getting confuse in start, stop and step method of the progress bar

Thanks a lot....;

Recommended Answers

All 13 Replies

Helloo sun

I had an exemple of a progress bar , this is it code , try to modify it depending on your needs , good luck :) :

#!/usr/bin/python

# progressbar + parcourir

from Tkinter import *
from os import getcwd
from tkFileDialog import askdirectory
from time import *

class Parcourir(Frame):
    def __init__(self, master = None, command = None, **kw):
        Frame.__init__(self, master, **kw)

        self.chemin = StringVar()
        self.chemin.set(getcwd())

        self.command = command

        self.entree_chemin = Entry(self, text = self.chemin, width = 70)
        self.entree_chemin.grid(row = 1, column = 1, padx = 5, pady = 5)

        self.bouton = Button(self, text = "Parcourir", command = self.parcours)
        self.bouton.grid(row = 1, column = 2, padx = 5, pady = 5)

    def parcours(self):
         chemin = askdirectory()
         if chemin != '':
             self.chemin.set(chemin)
             if self.command != None:
                 self.command()

    def get(self):
         return str(self.chemin.get())


class ProgressBar(Canvas):
     def __init__(self, master = None, **kw):
         Canvas.__init__(self, master,**kw)
         self.largeur = float(self.config()["width"][-1])+2
         self.hauteur = float(self.config()["height"][-1])+2

         self.valeur = 0
         self.barre = self.create_rectangle(0,0, self.largeur*self.valeur/100.,self.hauteur, fill = "Light Blue")
         self.texte = self.create_text(self.largeur/2. ,self.hauteur/2., text = str(int(self.valeur))+" %")

     def set_value(self, valeur):
         if 0<valeur<=100:
             self.valeur = valeur
             self.coords(self.barre, 0,0, self.largeur*self.valeur/100.,self.hauteur)
             valeur = str(float(valeur))
             self.itemconfig(self.texte, text = str(valeur)[:str(valeur).index('.')+3]+" %")
         else:
             raise ValueError, "0<valeur<=100"


def demo_bar():
     fen_bar = Toplevel(fen)
     fen_bar.grab_set()

     progress = ProgressBar(fen_bar, height = 25)
     progress.grid(row = 1, column = 1, sticky = W+E)


     for i in range(1,101):
         progress.set_value(i)
         fen_bar.update()
         sleep(0.1)


def demo_parcours():
     global label_parcours, parcours

     fen_parcours = Toplevel(fen)
     fen_parcours.grab_set()

     label_parcours = Label(fen_parcours)
     label_parcours.grid(row = 0, column = 1)

     parcours = Parcourir(fen_parcours, command = modify_label, border = 2, relief = RAISED)
     parcours.grid(row = 1, column = 1)
     modify_label()


def modify_label():
     label_parcours.config(text = parcours.get())

if __name__ == '__main__':
     fen = Tk()


     Button(fen, text = "Demo ProgressBar", command = demo_bar).grid(row = 1, column = 1, padx = 5, pady = 5)
     Button(fen, text = "Demo Parcours", command = demo_parcours).grid(row = 2, column = 1, padx = 5, pady = 5)


     fen.mainloop()

Thanks a lot Ismatus.....as i understand this code, u used canvas in making progress bar, i am looking for ttk module progress bar...if you have some example of it....

thx a lot for this code...

You are welcome Sun , I just remembered that i saved this code when i was reading about Python :) , if i'll face a ttk progress bar i tell you , you are welcome again , good luck .

Thanks a lot,,,,.. will wait for your reply.. :-)
I am also trying to do it using ttk, if i able to do it correctly, will post here....

Thx..

Vegaseat's example from this forum

# explore the ttk.Progressbar of the Tkinter module ttk
# ttk is included with Python 3.1.1
 
import tkinter as tk
from tkinter import ttk
 
def click(event):
    # can be a float
    increment = 4
    pbar.step(increment)
 
 
root = tk.Tk()
root.title('ttk.Progressbar')
 
pbar = ttk.Progressbar(root, length=300)
pbar.pack(padx=5, pady=5)
 
btn = tk.Button(root, text="Click to advance progress bar")
# bind to left mouse button click
btn.bind("<Button-1>", click)
btn.pack(pady=10)
 
root.mainloop()

Thanks a lot wooee... this is very helpful......but just one doubt if u could sort that out,,,,..

In determinate mode there is a option "value"..... which should be the dynamic variable whose value should be changed......

where i am getting stuck is .....
value_progress is a IntVar() variable, whose value is changing dynamically.....This thing work with the label but in progress bar it is giving error

Error:-
TclError: expected floating-point number but got "PY_VAR0"

l1 = Label(app, textvariable = value_progress)
            l1.pack(side = "top")
            l1.place(bordermode=OUTSIDE, x=60, y=10)

            progressbar = t.Progressbar(orient=HORIZONTAL, length=380, mode='determinate', value = value_progress)
            progressbar.pack(side="right")
            progressbar.place(bordermode=OUTSIDE, x=50, y=200)

Hello sun , what i know that in Python you can force to change the type of the variable when you need to use it for exemple :

#!/usr/bin/env python
print "Hiii Sun :) " 
a = "123"
print a + "ok"
b = int(a) + 1 # or b = float(a) + 1
print b

so i think you see in the error what is the type of the variable , and use : int() , float() , str() .. , good luck .

Thanks for reply, i m agree with you what you said....but that is not working with my progress bar....

I am new in python, so might be my code is little bit wrong so correct it, if u can.

#!/usr/bin/python
import pexpect
import os, sys
import shutil
import subprocess as sp
import _mysql
import MySQLdb as mdb
from Tkinter import *
import tkMessageBox
import ttk as t
import time
import thread
import Queue
import threading

def fun_exit():
        sys.exit()

def progress_bar():
        xit_btn.destroy()
        next_button.destroy()
        time.sleep(10)
        l1 = Label(app, textvariable = value_progress)
        l1.pack(side = "top")
        l1.place(bordermode=OUTSIDE, x=60, y=10)
        progressbar = t.Progressbar(orient=HORIZONTAL, length=380, mode='determinate', value=value_progress)
        progressbar.pack(side="right")
        progressbar.place(bordermode=OUTSIDE, x=50, y=200)

def cal_value():
        time.sleep(3)
        while not queue.empty():
                b = queue.get()
                value_progress.set(float(b))
                time.sleep(4)

def val_calculation():
        for i in range(20):
                queue.put(i)


app = Tk()
app.title("Example")
app.geometry('500x400+200+100')
#Exit button of the 1st Window
xit_btn = Button(app, text = 'Exit', width=10, command = fun_exit)
xit_btn.pack(side = 'right')
xit_btn.place(bordermode=OUTSIDE, x=250, y=350)
#Next button of the 1st window
next_button = Button(app, text = 'Next', width=10, command = progress_bar)
next_button.pack(side = 'right')
next_button.place(bordermode=OUTSIDE, x=370, y=350)

queue=Queue.Queue()
s=0
lbl1 = Label(app, textvariable=s)
lbl1.pack()
value_progress = StringVar()
value_progress.set(float('1.0'))
progressbar_thread = threading.Thread(name='progressbar_thread', target=val_calculation)
progressbar_thread.start()
cal_thread = threading.Thread(name='cal_thread', target=cal_value)
cal_thread.start()
app.mainloop()

I tried what you said, by changing it to float, but it didnt seems to work....

Thanks

Thanks Ismatus and wooee..... i finally find out the solution of it......i am taking the wrong option for progress bar instead of taking the value i need to take the variable option.....

progressbar = t.Progressbar(orient=HORIZONTAL, length=380, mode='determinate', variable=value_progress)
progressbar.pack(side="right")
progressbar.place(bordermode=OUTSIDE, x=50, y=200)

rest code is same ... and progress bar will work perfectly.....

Thanks again to those who reply.....

You are welcome sun , I've just wanted to try your code , ok , It is better that you could pass this probleme . I just wanted from you if you have time to explain to me :

progressbar.pack(side="right")
progressbar.place(bordermode=OUTSIDE, x=50, y=200)

why , you are using .pack() and .place() , thank you very much .

Hii again Sun , i want to ask again , how to mention here that the probleme is solved ? thank you very much

Yup sure, i always have time :-).... what i have done in this code is

pack and place are used to provide the location of the widget, means where we have to keep that widget in the main GUI window...... if you will use just pack option then it has only 4 options either top, left, right or bottom, as far as i understand it.....and if you use place option, then you can specify the particular location of the widget............and one more thing pack is a mandatory option, you need to specify it....


coming to my code ......

app = Tk()
app.title("Example")
app.geometry('500x400+200+100')
#Exit button of the 1st Window
xit_btn = Button(app, text = 'Exit', width=10, command = fun_exit)
xit_btn.pack(side = 'right')
xit_btn.place(bordermode=OUTSIDE, x=250, y=350)
#Next button of the 1st window
next_button = Button(app, text = 'Next', width=10, command = progress_bar)
next_button.pack(side = 'right')
next_button.place(bordermode=OUTSIDE, x=370, y=350)

queue=Queue.Queue()
s=0
lbl1 = Label(app, textvariable=s)
lbl1.pack()
value_progress = StringVar()
value_progress.set(1.0)
progressbar_thread = threading.Thread(name='progressbar_thread', target=val_calculation)
progressbar_thread.start()
cal_thread = threading.Thread(name='cal_thread', target=cal_value)
cal_thread.start()
app.mainloop()

This is the main function, i have used threading module to start the new thread(which will put the value in Queue)... Also specified a Queue to take the entry from thread and put them on the GUI...one thing i like to tell u, which i found interesting( maybe you are aware of it) is you cannot control anything of GUI from the thread.

def val_calculation():
        for i in range(100):
                queue.put(i)

this function i have used to put the value in the queue...this i have used to tell the progress bar how much work is complete.....

def cal_value():
        time.sleep(3)
        while not queue.empty():
                b = queue.get()
                value_progress.set(float(b))
                time.sleep(4)

This function is starting with little bit delay so that queue has some value, queue.get method is used to get the value from queue one by one...and the value_progress is a StingVar() variable whose value is changing dynamically....it can be IntVar()..

def progress_bar():
        xit_btn.destroy()
        next_button.destroy()
        time.sleep(10)
        l1 = Label(app, textvariable = value_progress)
        l1.pack(side = "top")
        l1.place(bordermode=OUTSIDE, x=60, y=10)
        progressbar = t.Progressbar(orient=HORIZONTAL, length=380, mode='determinate', variable=value_progress)
        progressbar.pack(side="right")
        progressbar.place(bordermode=OUTSIDE, x=50, y=200)
        time.sleep(2)

This is the main progress bar function... In this label i have used to show the current value in the progress bar and the progress bar "variable" option used to move the progress bar with the current value_progress option........


Hope i have explained correctly.......

and how to mention the thread is solved, there is option(or i say sentence is written, there a link (Mark Thread as solved)) at the bottom below the tab "USE Advance Editor".......

Okay thx for your help...and i hope our conversation would be helpful for others also....

Hello , you are too fast sun , it's good that you found the solution .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.