I’ve written some python 3.1 code (really simple as im no programmer) and I’m trying to put a GUI in it using tkinter 8.5 and python 3.1.4. The problem I’m having is that the progress bar won’t start. Here’s the part of the code:
def transformation(Xn,Yn,Zn,const):
infile = filedialog.askopenfile('r')
outfile = filedialog.asksaveasfile('w')
pbar.start()
for line in infile.readlines():
inlist = line.split(" ")
inlist = [float(i) for i in inlist]
l = (Xn+Yn+Zn)/const**2
Xm = inlist[0] + Xn*l
Ym = inlist[1] + Yn*l
Zm = inlist[2] + Zn*l
outlist=[0,0,0]
outlist[0] = inlist[0] + 2*(Xm-inlist[0])
outlist[1] = inlist[1] + 2*(Ym-inlist[1])
outlist[2] = inlist[2] + 2*(Zm-inlist[2])
outdata = str('%.4f' %outlist[0])+" "+str('%.4f' %outlist[1])+" "+str('%.4f' %outlist[2])+"\n"
outfile.writelines(outdata)
infile.close()
outfile.close()
pbar.stop()
The function is being called by a button. I give all the files needed for the programm to work. The calculation is successfully completed but the bar never starts. Any ideas?
Thanks,
AlexTh
The bar is starting, you’re just not seeing it because you don’t give the UI a chance to redraw. By the time the screen redraws you have stopped the progress bar. Redraws happen in response to redraw events, and those events get processed by the event loop. While your code is in the loop that reads the data you are preventing the event loop from running.
You will need to either a) use a thread or separate process to do your IO so as not to starve the event loop, b) you need to break your processing up into small chunks that can be done during each iteration of the event loop, or c) call
update_idletasksduring each iteration of your loop; this method processes “idle” events which includes screen refreshes.Google for “tkinter long running calculation” for lots of advice.