I have a tkinter GUI that allows users to perform searches on a sqlite database. The database is not static so it's unknown how long each search will take. The application is single threaded (and I'd like to keep it that way). I'd therefore like a progressbar (or something animated) to re-assure the user that something is happening while the search is going on. At first glance ttk.ProgressBar
looked like it might do the trick with its indeterminate
mode but it doesn't seem to work as detailed in TkDocs.
For indeterminate progress TkDocs states...
...at the start of the operation you'll just call the progressbar's "start" method, and at the end of the operation, you'll call its "stop" method. The progressbar will take care of the rest.
...but that doesn't seem to be the case. If I just use .start()
before I perform my sqlite query, and .stop()
once it's finished, the progress bar is present but doesn't animate at all.
I can force it to update if I execute the sqlite select statement in a for
loop and include a prog.step()
and a prog.update_idletasks()
. But this impacts performance and only really works if multiple lines are returned since the loop is for row in select_statement:
.
So how can I get an indeterminate progress bar (or animation) to animate on a single thread without forcing it with step
and update_idletask
?
The code. This doesn't work...
prog = ttk.ProgressBar(root,mode='indeterminate')
...
prog.start()
result = None
try:
conn = sqlite3.connect(database)
result = conn.execute('select * from table where a=? and b=?',(var1,var2))
result = result.fetchall()
conn.close()
except:
handle the exception
prog.stop()
Works-ish, but is hacky.
prog = ttk.ProgressBar(root,mode='indeterminate')
...
prog.start()
result = None
try:
for row in conn.execute('select * from table where a=? and b=?',(var1,var2)):
result.append(row)
prog.step()
prog.update_idletasks()
conn.close()
except:
handle the exception
prog.stop()