Welcome, guest | Sign In | My Account | Store | Cart

Progress bars are popular when trying to show how much of a job has been completed. In my case, it was encrypting and decrypting files. A GUI interface was written, but something was lacking. How was the user of the program supposed to know if the program was doing its job? A progress bar seemed like a good answer, so a simple GUI progress bar was written using Tkinter.

Python, 30 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Tkinter

################################################################################

class PB:

    # Create Progress Bar
    def __init__(self, width, height):
        self.__root = Tkinter.Toplevel()
        self.__root.resizable(False, False)
        self.__root.title('Progress Bar')
        self.__canvas = Tkinter.Canvas(self.__root, width=width, height=height)
        self.__canvas.grid()
        self.__width = width
        self.__height = height

    # Open Progress Bar
    def open(self):
        self.__root.deiconify()

    # Close Progress Bar
    def close(self):
        self.__root.withdraw()

    # Update Progress Bar
    def update(self, ratio):
        self.__canvas.delete(Tkinter.ALL)
        self.__canvas.create_rectangle(0, 0, self.__width * ratio, \
                                       self.__height, fill='blue')
        self.__root.update()

You will need to catch errors when working with this class. The window can still be closed.

1 comment

Danci Emanuel 14 years, 7 months ago  # | flag

I need some help using this class. I have to convert some files from a directory and I need to track the progress somehow. This is an awesome class but I have some some problem using it. I tried to do my job but I get nothing. I have a number of files (numFiles) in the directory and after each file is converted I need to update the progress. How do I write the code in order be able to use this class?

Thanks, Danci Emanuel