Monday, 9 September 2013

Avoid tkinter GUI lock when using ttk.Progressbar

Avoid tkinter GUI lock when using ttk.Progressbar

I'm trying to write a GUI for something long running and cant seem to
figure out how to avoid locking the GUI thread. I want to use
ttk.Progressbar but I cant seem to update the bar value and have the
window update the GUI. I've tried putting the update handling in its own
function and updating it directly but neither worked. Updating the value
in a handler is what I'd prefer to do since this script would do some
downloading, then processing, then uploading and three separate bars would
look nicest.
from Tkinter import *
import time, ttk
class SampleApp(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.pack()
self.prog = ttk.Progressbar(self, orient = 'horizontal', length =
200, mode = 'determinate')
self.prog.pack()
self.button = Button(self,text='start',command=self.start)
self.button.pack()
def start(self):
self.current = 0
self.prog["value"] = 0
self.max = 10
self.prog["maximum"] = self.max
self.main_prog()
def handler(self):
self.prog['value'] += 1
def main_prog(self):
for x in range(10):
time.sleep(2)
self.handler()
root = Tk()
app = SampleApp(master = root)
app.mainloop()

No comments:

Post a Comment