Is it possible to get the GUI to update variable 'a' to update in real time while 'thread2' increments the value?
import tkinter as tk
from threading import Thread
import time
a = 0 # global variable
def thread1(threadname):
root = tk.Tk()
w = tk.Label(root, text=a)
w.pack()
root.mainloop()
def thread2(threadname):
global a
while True:
a += 1
time.sleep(1)
thread1 = Thread( target=thread1, args=("Thread-1", ) )
thread2 = Thread( target=thread2, args=("Thread-2", ) )
thread1.start()
thread2.start()
If I create a loop and print 'a' I get the correct result.
def thread1(threadname):
global a
while True:
print(a)
# root = tk.Tk()
# w = tk.Label(root, text=a)
# w.pack()
# root.mainloop()
Any help would be appreciated
When you create your label, that's not a "live" connection. That passes the current value of the variable a. You then enter your main loop, and that thread does nothing else until the application exits. You need to send the new value to a function that executes as part of the main thread, and that function will need access to the label.
This works:
import tkinter as tk
from threading import Thread
import time
class GUI(object):
def __init__(self):
self.a = 0
self.w = tk.Label(root, text=self.a)
self.w.pack()
thread2 = Thread( target=self.thread2, args=("Thread-2", ) )
thread2.start()
def thread2(self,threadname):
while True:
self.a += 1
root.after_idle(self.update)
time.sleep(1)
def update(self):
self.w.config(text=self.a)
root = tk.Tk()
gui = GUI()
root.mainloop()
It is possible to make a live connection using textvariable, but then you have to change the type of a to a tkinter.StringVariable. Check here: Update Tkinter Label from variable
Try using a <tkinter.Tk>.after loop like this:
import tkinter as tk
def loop():
global a
a += 1
label.config(text=a)
# run `loop` again in 1000 ms
root.after(1000, loop)
a = 0
root = tk.Tk()
label = tk.Label(root, text=a)
label.pack()
loop() # Start the loop
root.mainloop()
I used a .after script because tkinter and threading don't go together very well. Sometimes tkinter can crash without even giving you an error message if you try to call some tkinter commands from the second thread.
Related
When using the Tkinter .after method, the code continues passed without waiting for the callback to complete.
import tkinter as tk
import tkinter.ttk as ttk
import time
from datetime import datetime
global i
i = 0
global j
j = 0
def SomeFunction():
global i
for num in range(10):
i+=1
x = barVar.get()
barVar.set(x+5)
histrun_mainWindow.update()
time.sleep(2)
def SecondFunction():
global j
for num in range(10):
j+=1
x = barVar.get()
barVar.set(x+5)
histrun_mainWindow.update()
time.sleep(2)
def Load(run_date):
histrun_mainWindow.after(50, SomeFunction)
histrun_mainWindow.after(50, SecondFunction)
global i, j
print 'Number is :', i + j
histrun_mainWindow = tk.Tk()
run_date = datetime.today().date()
barVar = tk.DoubleVar()
barVar.set(0)
bar = ttk.Progressbar(histrun_mainWindow, length=200, style='black.Horizontal.TProgressbar', variable=barVar, mode='determinate')
bar.grid(row=1, column=0)
button= tk.Button(histrun_mainWindow, text='Run for this date ' + str(run_date), command=lambda:Load(run_date))
button.grid(row=0, column=0)
histrun_mainWindow.mainloop()
This example shows what is happening. The .after() calls the Load() function but doesn't wait for Load() to complete, it goes straight on to the next line.
I want i to print as 10 but because the .after() doesn't wait for Load() to finish it's additions, i prints as 0
The progress bar continues to update so I know that Load was called as it continues in the background after i is printed
Question: the progress bar doesn't update - the window freezes until all functions have completed
Use a Thread to prevent main loop from freezing.
Your functions - SomeFunction, SecondFunction - may also in global namespace.
Then you have to pass self.pbar as paramter, e.g. SomeFunction(pbar): ... f(self.pbar).
Note:
You see a RuntimeError: main thread is not in main loop
if you .destroy() the App() window while the Thread is running!
import tkinter as tk
import threading
class App(tk.Tk):
def __init__(self):
super().__init__()
btn = tk.Button(self, text='Run',
command=lambda :threading.Thread(target=self.Load).start())
btn.grid(row=0, column=0)
self.pbar = ttk.Progressbar(self, maximum=2 *(5 * 5), mode='determinate')
self.pbar.grid(row=1, column=0)
def SomeFunction(self):
for num in range(5):
print('SomeFunction({})'.format(num))
self.pbar['value'] += 5
time.sleep(1)
return num
def SecondFunction(self):
for num in range(5):
print('SecondFunction({})'.format(num))
self.pbar['value'] += 5
time.sleep(1)
return num
def Load(self):
number = 0
for f in [self.SomeFunction, self.SecondFunction]:
number += f()
print('Number is :{}'.format(number))
if __name__ == "__main__":
App().mainloop()
Output:
SomeFunction(0)
SomeFunction(1)
SomeFunction(2)
SomeFunction(3)
SomeFunction(4)
SecondFunction(0)
SecondFunction(1)
SecondFunction(2)
SecondFunction(3)
SecondFunction(4)
Number is :8
Tested with Python: 3.5
*)Could not test with Python 2.7
I have tried to implement a Tkinter progressbar using threading simply to see when a program is running, and to close the progressbar when the program ends.
import tkinter
import ttk
import time
import threading
def task(root):
ft = ttk.Frame()
ft.pack(expand=True, fill=tkinter.BOTH, side=tkinter.TOP)
pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')
pb_hD.pack(expand=True, fill=tkinter.BOTH, side=tkinter.TOP)
pb_hD.start(50)
root.mainloop()
def process_of_unknown_duration(root):
time.sleep(5)
root.destroy()
def pBar():
root = tkinter.Tk()
t1=threading.Thread(target=process_of_unknown_duration, args=(root,))
t1.start()
task(root) # This will block while the mainloop runs
t1.join()
if __name__ == '__main__':
pBar()
#some function
My issue is that once the progressbar starts, the program just hangs and wont do anything else. Any hints?
This is because your call root.mainloop() is blocking the execution of your code. It basically represents the loop for your UI. You might want to look at this answer for a progress bar that is launched by a button.
Are you still interested in this problem? Try to use the update() method for the root object instead of the threading in the simple cases. I offer the following simplified demo-solution with global variables as a start point for subsequent developments.
import tkinter
from tkinter import *
from tkinter import ttk
import time
root1 = Tk()
progrBar1 = None # The main progress bar
win2 = None
def click_but1():
global win2, progrBar2
if win2 is None:
win2 = Toplevel() # Secondary window
win2.title('Secondary')
win2.protocol('WM_DELETE_WINDOW', clickClose) # close the secondary window on [x] pressing
but2 = Button(win2, text='Close', command=clickClose)
but2.pack()
but3 = Button(win2, text='Process', command=clickProcess)
but3.pack()
progrBar2 = ttk.Progressbar(win2, orient = 'horizontal', length = 300, mode = 'determinate')
progrBar2.pack()
if progrBar1:
progrBar1.start(50)
def clickClose():
global win2
progrBar1.stop()
win2.destroy()
win2=None
def clickProcess():
my_func()
def my_func():
global progrBar2
range1, range2 = 20, 40
step1 = 100/range1
for i in range(range1):
for j in range(range2):
time.sleep(0.01)
progrBar2.step(step1)
root1.update() # the "update" method
root1.title('Root') # Main window
progrBar1 = ttk.Progressbar(root1, orient = 'horizontal', mode = 'indeterminate') # The main progress bar
but1 = Button(root1, text = 'Start', command = click_but1)
but1.pack()
progrBar1.pack()
root1.mainloop()
The progress bar in the first (main) window is moving only in the presence of the secondary window. This pogress bar stops and returns to the initial position after closing of the secondary window. The secondary window has it own progress bar for the demo purposes and to show the interaction between windows by means of the update() method.
I am running a thread to constantly monitor temperature, and update a global variable that other functions call upon to perform their tasks accordingly. Everything works now, and I could live with it. But I wanted to reach out and see if anyone has a smarter way to keep this going until the mainloop ends. Here is how the thread is started...
thread1 = Thread(target = test)
try:
thread1.start()
except (KeyboardInterrupt, SystemExit):
thread1.stop()
sys.exit()
I guess I could make a small script just to let it run. Sorry...
import time
import tkinter
from tkinter import *
from threading import Thread
root = Tk()
timer = True
global counter
counter = 0
var1 = StringVar()
var1.set(counter)
def test():
global counter
while timer:
counter += 1
var1.set(counter)
root.update
time.sleep(1)
testtext = Label(root,textvariable=var1,font='Consolas 24 bold')
testtext.grid(row=1,column=1,sticky="N,S,E,W",padx=10,pady=0)
testtext2 = Label(root,text="SECONDS",font='Consolas 18 bold')
testtext2.grid(row=2,column=1,sticky="N,S,E,W",padx=10,pady=0)
thread1 = Thread(target = test)
try:
thread1.start()
except (KeyboardInterrupt, SystemExit):
thread1.stop()
sys.exit()
root.mainloop()
As you will see, after you close the window, the thread does end, but not very cleanly.
Any ideas?
Tkinter supports protocol handlers.
The most commonly used protocol is called WM_DELETE_WINDOW, and is used to define what happens when the user explicitly closes a window using the window manager.
import time
import tkinter
from tkinter import *
from threading import Thread
def on_closing():
thread1.close()
root.destroy()
def test():
global counter
while timer:
counter += 1
var1.set(counter)
root.update
time.sleep(1)
root = Tk()
timer = True
global counter
counter = 0
var1 = StringVar()
var1.set(counter)
testtext = Label(root,textvariable=var1,font='Consolas 24 bold')
testtext.grid(row=1,column=1,sticky="N,S,E,W",padx=10,pady=0)
testtext2 = Label(root,text="SECONDS",font='Consolas 18 bold')
testtext2.grid(row=2,column=1,sticky="N,S,E,W",padx=10,pady=0)
global thread1 = Thread(target = test)
try:
thread1.start()
except (KeyboardInterrupt, SystemExit):
thread1.stop()
sys.exit()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
Basically, the program listens to the close event and decides what to do as the window is killed in the functuon on_closing().
Got it. Thank you Daniel Reyhanian for pointing me toward the
root.protocol("WM_DELETE_WINDOW", on_closing)
thing. Never seen that before.
Here is the final bit using: os._exit(1)
import os
import time
import tkinter
from tkinter import *
from threading import Thread
root = Tk()
timer = True
global counter
counter = 0
var1 = StringVar()
var1.set(counter)
def on_closing():
os._exit(1)
def test():
global counter
while timer:
counter += 1
var1.set(counter)
root.update
time.sleep(1)
testtext = Label(root,textvariable=var1,font='Consolas 24 bold')
testtext.grid(row=1,column=1,sticky="N,S,E,W",padx=10,pady=0)
testtext2 = Label(root,text="SECONDS",font='Consolas 18 bold')
testtext2.grid(row=2,column=1,sticky="N,S,E,W",padx=10,pady=0)
thread1 = Thread(target = test)
thread1.start()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
I am trying to have a ttk progressbar call a function that adds to a value that is displayed in the Tkinter window. I currently have a function that is called at the same time the progressbar begins:
def bar1_addVal():
global userMoney
userMoney += progBar1_values.value
moneyLabel["text"] = ('$' + str(userMoney))
canvas1.after((progBar1_values.duration*100), bar1_addVal)
return
but I cannot seem to get the exact amount of time it takes for the progressbar to finish each iteration. Is there a way to have the progressbar call a function every time it completes?
You can use threading to check for the variable in a loop. Then you wont interrupt the main loop.
I made a little example of this:
import threading, time
from ttk import Progressbar, Frame
from Tkinter import IntVar, Tk
root = Tk()
class Progress:
val = IntVar()
ft = Frame()
ft.pack(expand=True)
kill_threads = False # variable to see if threads should be killed
def __init__(self):
self.pb = Progressbar(self.ft, orient="horizontal", mode="determinate", variable=self.val)
self.pb.pack(expand=True)
self.pb.start(50)
threading.Thread(target=self.check_progress).start()
def check_progress(self):
while True:
if self.kill_threads: # if window is closed
return # return out of thread
val = self.val.get()
print(val)
if val > 97:
self.finish()
return
time.sleep(0.1)
def finish(self):
self.ft.pack_forget()
print("Finish!")
progressbar = Progress()
def on_closing(): # function run when closing
progressbar.kill_threads = True # set the kill_thread attribute to tru
time.sleep(0.1) # wait to make sure that the loop reached the if statement
root.destroy() # then destroy the window
root.protocol("WM_DELETE_WINDOW", on_closing) # bind a function to close button
root.mainloop()
Edit: Updated the answer, to end the thread before closing window.
I'm trying to write a Python GUI program with tkinter.
I want to make two thread. One runing with the main_form function to keep tkinter from keep update and loop (avoid "Not Responding").
The other, when the button1 (btn1) is clicked make function sci_thread() start running and start thread2 that execute the main_scikit with long time code.
But tkinter keep Not Responding.
Below is my code:
import threading
class class_one:
def main_scikit(seft):
######
code_take_loooong_time
######
def save(seft):
pass
def main_form(seft):
root = Tk( )
root.minsize(width=300, height=500)
ent1 = Entry(width=30)
ent1.grid(row=0,column=1,padx = 10,pady=5)
bnt1 = Button(root,text = "Start",command=lambda : seft.sci_thread())
bnt1.grid(row=5,column=0,padx = 10)
root.update()
root.mainloop()
def sci_thread(seft):
maincal = threading.Thread(2,seft.main_scikit())
maincal.start()
co = class_one()
mainfz = threading.Thread(1,co.main_form());
mainfz.start()
Your app is unresponsive because your target parameter executed when declared and result of that passed as target. And, obviously, because of that you GUI is unresponsive while code_take_loooong_time being executed in GUI's thread. To deal with it - get rid of redundant parentheses.
Try this snippet:
import threading
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
class class_one:
def main_scikit(self):
######
# code_take_loooong_time
# same as sleep
threading.Event().wait(5)
# some print
self.print_active_threads_count()
######
def save(self):
pass
def main_form(self):
self.root = tk.Tk()
self.root.minsize(width=300, height=500)
self.ent1 = tk.Entry(self.root, width=30)
self.ent1.grid(row=0, column=1, padx=10, pady=5)
self.bnt1 = tk.Button(self.root, text="Start", command=self.sci_thread)
self.bnt1.grid(row=5, column=0, padx=10)
self.root.update()
self.root.mainloop()
def sci_thread(self):
maincal = threading.Thread(target=self.main_scikit)
maincal.start()
def print_active_threads_count(self):
msg = 'Active threads: %d ' % threading.active_count()
self.ent1.delete(0, 'end')
self.ent1.insert(0, msg)
print(msg)
co = class_one()
mainfz = threading.Thread(target=co.main_form)
mainfz.start()
Links:
Similar problem with a tkinter button's parameter
More general: How to pass a method as an parameter
Threading docs
P.S.:
Also, be careful when you start a tkinter application not in the main thread because tkinter expects (in general) that mainloop is outer-most loop possible and that all Tcl commands invoked from the same thread. So there can be many and more synchronisation problem with all that, even if you just trying to quit GUI!
In conclusion, maybe this and that would give you some new ideas.