The following code should update the GUI while at the same time running a calculation. IN this case self.after() is not a solution as the calculation will be ongoing for a long time (and also there has to be time.sleep(n) in the calculation, but this should not stop the GUI from working (i.e. user can still adjust sliders). So it has to be done via multiple threads. The following code is what I would like to suggest, but it doesn't work. Any suggestions what I got wrong?
import Tkinter as tk
import ttk
import time
import threading
class GUI(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.i=0
self.var1 = tk.StringVar()
self.var2 = tk.StringVar()
self.var3 = tk.StringVar()
self.var4 = tk.StringVar()
self.var5 = tk.StringVar()
self.var6 = tk.StringVar()
self.var7 = tk.StringVar()
tk.Label(self, textvariable=self.var1,justify='left').pack()
tk.Label(self, textvariable=self.var2).pack()
tk.Label(self, textvariable=self.var3).pack()
tk.Label(self, textvariable=self.var4).pack()
tk.Label(self, textvariable=self.var5).pack()
tk.Label(self, textvariable=self.var6).pack()
tk.Label(self, textvariable=self.var7).pack()
p1 = tk.Scale(master, from_=1, to=20, orient=tk.HORIZONTAL).pack()
p2 = tk.Scale(master, from_=1, to=20, orient=tk.HORIZONTAL).pack()
p3 = tk.Scale(master, from_=1, to=20, orient=tk.HORIZONTAL).pack()
p4 = tk.Scale(master, from_=1, to=20, orient=tk.HORIZONTAL).pack()
p5 = tk.Scale(master, from_=1, to=20, orient=tk.HORIZONTAL).pack()
self.progress = ttk.Progressbar(self, orient="horizontal",length=100, mode="determinate")
self.progress.pack()
def calculation(self):
while True:
# complex calculations that last 30 minutes in total
self.i += 1
time.sleep(1)
max=1000
self.var1.set("Value1: "+str(self.i))
self.var2.set("Value2: "+str(self.i+100))
self.var3.set("Value3: "+str(self.i+100))
self.var4.set("Value4: "+str(self.i+100))
self.var5.set("Value5: "+str(self.i+100))
self.var6.set("Value6: "+str(self.i+100))
self.var7.set("Value7: "+str(self.i+100))
self.progress["value"] = int(round(self.i*100/max))
#self.update() # no need, will be automatic as mainloop() runs
#self.after(1, self.calculation) not necessary anymore as everything is in a main loop
app=GUI()
t1 = threading.Thread(target=app.calculation, args=[])
t2 = threading.Thread(target=app.mainloop, args=[])
t1.start()
t2.start()
t1.join()
t2.join()
print ("thread finished...exiting")