Discussion:
tkinter progress bar
(too old to reply)
h***@walla.com
2013-07-23 06:52:42 UTC
Permalink
Hi,

How can I add a tkinter progress bar in python 3.2 to start before a loop and end after it. I am looking for a very simple solution.

def MyFunc():
Start progress bar

for fileName in fileList:


End progress bar


Thanks a lot in advance.
Christian Gollwitzer
2013-07-23 08:43:02 UTC
Permalink
Post by h***@walla.com
Hi,
How can I add a tkinter progress bar in python 3.2 to start before a loop and end after it. I am looking for a very simple solution.
Start progress bar

End progress bar
1. There is a progress bar widget in ttk. At the beginning, you set
maximum to the number of files in your list

2. In the loop, you set "value" of the progressbar to the current file
number. You can also attach a variable

3. The bar is only redrawn when you process events. The simplest way to
do this is by calling update() on the progress bar, which processes all
pending events. Despite of it looking like a method, update() is really
a global function within Tcl and updates all widgets in your interface.
You must make sure, therefore, that the user does not trigger another
event which interferes with your download, such as pressing the button
for starting it again. The easiest way is to disable the button at the
begin and reenable it at the end.

4. If processing of a single file takes a long time, the only way to
have the GUI responsive is to put the work in a background thread. That
seems to be more involved.

Christian
h***@walla.com
2013-07-23 09:38:34 UTC
Permalink
Dear Christian,

Thanks for the help. Can you please add a source example as I am new with Tkinter.

Cheers.
Jason Swails
2013-07-23 13:27:59 UTC
Permalink
Post by h***@walla.com
Dear Christian,
Thanks for the help. Can you please add a source example as I am new with Tkinter.
http://docs.python.org/2/library/ttk.html#progressbar

You can do something like this:

#!/usr/bin/env python

import Tkinter as tk
import ttk
import time

class MainApp(tk.Frame):

def __init__(self, master):
tk.Frame.__init__(self, master)
self.progress = ttk.Progressbar(self, maximum=10)
self.progress.pack(expand=1, fill=tk.BOTH)
self.progress.bind("<Button-1>", self._loop_progress)

def _loop_progress(self, *args):
for i in range(10):
self.progress.step(1)
# Necessary to update the progress bar appearance
self.update()
# Busy-wait
time.sleep(2)


if __name__ == '__main__':
root = tk.Tk()
app = MainApp(root)
app.pack(expand=1, fill=tk.BOTH)
root.mainloop()


This is a simple stand-alone app that (just) demonstrates how to use the
ttk.Progressbar widget.

HTH,
Jason

Loading...