Prevent a queue of events in tkinter - python

I have made a GUI using TkInter.
Now I am trying to perform an action after a key press.
However, I don't want the action to perform twice when I click the button twice.
But rather, the program should wait until the previous action is finished before it starts waiting for the next key press.
I cannot seem to achieve this, even thought the command handler returns "break".
See below a minimal working example, where if you press the <Left> button multiple times, the code also executes multiple times. How should I change the code to achieve the desired performance?
import tkinter as tk
import time
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self)
# Create handler
def event_handler(event):
print('One run for event', event)
time.sleep(1)
print('Done')
return "break"
# Button
self.button = tk.Button(
master=self,
text='Test',
command = event_handler
)
self.button.grid()
self.bind('<Left>', event_handler)
self.focus()
if __name__ == "__main__":
root = tk.Tk()
root.title("TestTool")
# Start application
app = App(root)
app.pack(fill="both", expand=True)
root.mainloop()

When You execute Your event_handler with time.sleep(1), You are effectively blocking any other event execution. But event triggers are accumulating and executed one after another how each event_handler starts and ends. It creates effect of serial execution of event_handler.
What You want is to set some flag which will indicates Your event state. Then You have to process all accumulated events before Your return from event_handler. This will cause all events to execute, but immediately return, because flag is set to True: event is running.
Here is code snippet:
import time
import tkinter as tk
class App(tk.Frame):
event_running = False
def __init__(self, parent):
tk.Frame.__init__(self)
# Create handler
def event_handler(event=None):
# Check if event is running
if self.event_running:
return False
# Lock event until it's finished
self.event_running = True
print('One run for event', event)
time.sleep(1)
print('Done')
# Update all pending events
self.update()
self.update_idletasks()
# Release event handler
self.event_running = False
return
# Button
self.button = tk.Button(
master=self,
text='Test',
command=event_handler
)
self.button.grid()
self.bind('<Left>', event_handler)
self.focus()
if __name__ == "__main__":
root = tk.Tk()
root.title("TestTool")
# Start application
app = App(root)
app.pack(fill="both", expand=True)
root.mainloop()

Related

Python Multithreading - not running simultaneously

im trying to learn python. But i have problems with the threading. First i failed in the "Proces" class because i putted the loop on the wrong place and my program newer returned from the other class.
But now i think all is correct and it still does not work. I need to have a GUI where i want to be able to write my conditions via text entries and i need another class "Proces" that will do stuff, checking status ower internet and so on constantly or in a specified interval...
The Problem is that my tkinter GUI is freezing after pressing something
here is my GUI.py file:
import tkinter as tk
from Proces import Proces
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame, text="QUIT", fg="red",command=quit).pack(side=tk.LEFT)
pr = Proces()
print("\nGUI: proces init...")
pr.start()
print("\nGUI: Start ended")
root.mainloop()
here is the Proces.py file:
import time, threading
class Proces(threading.Thread):
def loop(self):
while True:
time.sleep(2)
print("\nProces: looping")
def __init__(self):
threading.Thread.__init__(self)
print("\nProces: Starting proces")
time.sleep(2)
def run(self):
self.deamon = True
print("\nProces: Starting loop")
self.loop()
*This is the output: *
Proces: Starting proces
GUI: proces init...
Proces: Starting loop
GUI: Start ended
Proces: looping
Proces: looping
Proces: looping
Proces: looping
*But the GUI of the tkinter does not react.*
How should i do this kind of task?
Thank you for your help, advice and answer
I think you don't have problem about starting thread etc. However, you should be able to control your thread, meaning your thread should return based on some condition. It seems you have a button to quit. I assume you want to finish the process by clicking the button.
To do that, when you click to button, your main thread should pass a variable to Process in order to break your while loop, which is your thread basically.
Here is a code that you can work on.
import tkinter as tk
import time
import threading
class MainWindow:
def __init__(self, master):
self.master = master
self.quit_button = tk.Button(self.master, text="QUIT", command=lambda:self.quit(), width=20)
self.quit_button.pack(side=tk.LEFT)
self.process = None
print("\nGUI: process init...")
self.start_task()
def quit(self):
self.process.stop_process = True
print("\nGUI: Start ended")
def start_task(self):
self.process = Process()
self.process.start()
class Process(threading.Thread):
def loop(self):
while True:
if not self.stop_process:
time.sleep(2)
print("\nProcess: looping")
else:
print("\nProcess: looping ended")
return
def __init__(self):
threading.Thread.__init__(self)
self.stop_process = False
print("\nProcess: Starting proces")
time.sleep(2)
def run(self):
self.deamon = True
print("\nProcess: Starting loop")
self.loop()
if __name__ == '__main__':
root = tk.Tk()
app = MainWindow(master=root)
root.mainloop()
So you start yout tkinter, which is your main thread. Then iniating another class within your main thread, which is inherits the thread. So you have two seperate thread that working. When you clicked to "quit" button, you pass variable stop_process, which breaks the loop and return, meaning ending your thread. Your main thread is still alive for running your tkinter window.
I hope it will help

Trying to keep a function constantly running while tkinter button is held

I currently have a button in tkinter to run a function when the button is released. I need the button to constantly add toa number at a certain rate the entire time the button is being held.
global var
var=1
def start_add(event,var):
global running
running = True
var=var+1
print(var)
return var
def stop_add(event):
global running
print("Released")
running = False
button = Button(window, text ="Hold")
button.grid(row=5,column=0)
button.bind('<ButtonPress-1>',start_add)
button.bind('<ButtonRelease-1>',stop_add)
i dont necessarily need any function to run when the button is released, just while the button is being held if this helps. Any help is much appreciated.
There is nothing builtin that can do this, but it would be easy to make your own Button that can. You are on the right track too, only thing you are missing is that you need to use after to make the loop and after_cancel to stop the loop:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
class PaulButton(tk.Button):
"""
a new kind of Button that calls the command repeatedly while the Button is held
:command: the function to run
:timeout: the number of milliseconds between :command: calls
if timeout is not supplied, this Button runs the function once on the DOWN click,
unlike a normal Button, which runs on release
"""
def __init__(self, master=None, **kwargs):
self.command = kwargs.pop('command', None)
self.timeout = kwargs.pop('timeout', None)
tk.Button.__init__(self, master, **kwargs)
self.bind('<ButtonPress-1>', self.start)
self.bind('<ButtonRelease-1>', self.stop)
self.timer = ''
def start(self, event=None):
if self.command is not None:
self.command()
if self.timeout is not None:
self.timer = self.after(self.timeout, self.start)
def stop(self, event=None):
self.after_cancel(self.timer)
#demo code:
var=0
def func():
global var
var=var+1
print(var)
root = tk.Tk()
btn = PaulButton(root, command=func, timeout=100, text="Click and hold to repeat!")
btn.pack(fill=tk.X)
btn = PaulButton(root, command=func, text="Click to run once!")
btn.pack(fill=tk.X)
btn = tk.Button(root, command=func, text="Normal Button.")
btn.pack(fill=tk.X)
root.mainloop()
As #rioV8 mentioned, the after() call is not extremely accurate. If you set the timeout to 100 milliseconds, you can usually expect anywhere from 100 - 103 milliseconds between calls. These errors will stack up the longer the button is held. If you are trying to time exactly how long the button has been held down you will need a different approach.
#Novel's answer should work I think, but here is something more along the lines of what you were trying, that doesn't require a whole new class:
from tkinter import *
INTERVAL=5 #miliseconds between runs
var=1
def run():
global running, var
if running:
var+=1
print(var)
window.after(INTERVAL, run)
def start_add(event):
global running
running = True
run()
def stop_add(event):
global running, var
print("Released")
running = False
window=Tk()
button = Button(window, text ="Hold")
button.grid(row=5,column=0)
button.bind('<ButtonPress-1>',start_add)
button.bind('<ButtonRelease-1>',stop_add)
mainloop()

Change tkinter Label in other class?

Can somebody help me please, I'm making an exercise about class and running task on other thread then tkinter. I want to change the label in another class. Can't get my script to work.
I tried different things but I'm having some troubles with understanding the inheriting from classes and the threads, so this is just an example to learn more about it.
from tkinter import *
import tkinter as tk
from tkinter import ttk
import threading
#Gloabl for stopping the run task
running = True
#class 1 with window
class App():
def __init__(self):
#making the window
self.root = tk.Tk()
self.root.geometry("400x400+300+300")
self.root.protocol("WM_DELETE_WINDOW", self.callback)
self.widgets()
self.root.mainloop()
# stop task and close window
def callback(self):
global running
running = False
self.root.destroy()
# all the widgets of the window
def widgets(self):
global labelvar
#startbutton
self.start_button = tk.Button(self.root, text="Start", command=lambda:App2())
self.start_button.pack()
#stopbutton
self.stop_button = tk.Button(self.root, text="Stop", command=lambda:self.stop())
self.stop_button.pack()
#Defining variable for text for label
labelvar = "Press start to start running"
self.label = tk.Label(self.root, text=labelvar)
self.label.pack()
#stop the task
def stop(self):
global running
running = False
#class 2 with task in other thread
class App2(threading.Thread):
def __init__(self):
global running
#check if task can be run
running = True
threading.Thread.__init__(self)
self.start()
def run(self):
#starting random work
for i in range(10000):
print(i)
labelvar = "running"
App.label.pack()
#checking if task can still be running else stop task
if running == False:
break
labelvar = "stopped"
App.label.pack()
#initiate main app
app = App()
As I said in a comment, tkinter doesn't support multithreading itself, but you can do it as long as only one thread, usually the main one, uses (or "talks") it.
If you want to affect what the GUI displays, the other thread(s) must communicate somehow with the GUI thread. This is often done through a queue.Queue, but in this relatively simple case it can be done through a global variable provided that concurrent access to it is controlled by some means—sharing memory space (i.e. global variables) is one of the advantages of multithreading vs multitasking, but it has to be done and done correctly.
An easy way to share a resource like this is by using a threading.Lock dedicated for that purpose. (See the Wikipedia article Lock (computer science) for more details.)
All references to this shared resource (the running flag) should only be done after "acquiring" the Lock and "releasing" it afterwards. Fortunately it's trivial to do this using a Python with statement (as shown below).
Another crucial aspect of the multithreading problem is how any information exchanged between the two threads is processed. In this case I choose to make the tkinter thread poll the running flag, watch for changes, and update any affected widgets accordingly. This can be done by using the universal widget method after() which tells tkinter to schedule a future call (inside the 'mainloop') to a user-supplied function or method and to pass it certain arguments. To get this to happen repeatedly, the called function can reschedule itself to run again by calling after() before it finishes.
Below is a modified version of your code that does these thing. Note that App2 never calls tkinter or touches any of its widgets, which is why it works.
import threading
from time import sleep
from tkinter import *
import tkinter as tk
from tkinter import ttk
DELAY = 100 # millisecs between status label updates
# global flag and a Lock to control concurrent access to it
run_flag_lock = threading.Lock()
running = False
# class 1 with window
class App():
def __init__(self):
global running
self.root = tk.Tk()
self.root.geometry("400x400+300+300")
self.root.protocol("WM_DELETE_WINDOW", self.quit)
self.create_widgets()
with run_flag_lock:
running = False
self.root.after(DELAY, self.update_status, None) # start status widget updating
self.root.mainloop()
# create all window widgets
def create_widgets(self):
self.start_button = tk.Button(self.root, text="Start", command=self.start)
self.start_button.pack()
self.stop_button = tk.Button(self.root, text="Stop", command=self.stop)
self.stop_button.pack()
self.status_label = tk.Label(self.root, text='')
self.status_label.pack()
def update_status(self, run_state):
""" Update status label text and state of buttons to match running flag. """
# no need to declare run_flag_lock global since it's not being assigned a value
with run_flag_lock:
if running != run_state: # status change?
if running:
status_text = 'Press Stop button to stop task'
run_state = True
else:
status_text = 'Press Start button to start task'
run_state = False
self.status_label.config(text=status_text)
# also update status of buttons
if run_state:
self.start_button.config(state=DISABLED)
self.stop_button.config(state=ACTIVE)
else:
self.start_button.config(state=ACTIVE)
self.stop_button.config(state=DISABLED)
# run again after a delay to repeat status check
self.root.after(DELAY, self.update_status, run_state)
# start the task
def start(self):
global running
with run_flag_lock:
if not running:
app2 = App2() # create task thread
app2.start()
running = True
# stop the task
def stop(self):
global running
with run_flag_lock:
if running:
running = False
# teminate GUI and stop task if it's running
def quit(self):
global running
with run_flag_lock:
if running:
running = False
self.root.destroy()
# class 2 with task in another thread
class App2(threading.Thread):
def __init__(self):
super(App2, self).__init__() # base class initialization
self.daemon = True # allow main thread to terminate even if this one is running
def run(self):
global running
# random work
for i in range(10000):
print(i)
# Normally you shouldn't use sleep() in a tkinter app, but since this is in
# a separate thread, it's OK to do so.
sleep(.25) # slow printing down a little
# stop running if running flag is set to false
with run_flag_lock:
if not running:
break # stop early
with run_flag_lock:
running = False # task finished
# create (and start) main GUI app
app = App()

multithreading from a tkinter app

I have a tkinter application that runs on the main thread. After receiving some input from the user, a new thread is created to perform functions in a separate class. The main thread continues to a Toplevel progress window.
My question is, how can I communicate to the main thread when the second thread has finished its tasks, in order to close the progress window?
import tkinter as tk
from tkinter import ttk
from threading import Thread
import time
class Application:
def __init__(self, master):
# set main window
frame = tk.Frame(master, width=300, height=100)
frame.pack(fill=tk.BOTH)
# button widget
run_button = tk.Button(frame, text="GO", command=self.do_something)
run_button.pack()
# simulate some gui input from user
self.user_input = "specified by user"
def do_something(self):
thread1 = Thread(target=FunctionClass, args=(self.user_input,))
thread1.start() # launch thread
ProgressWindow() # main thread continues to new tkinter window
class ProgressWindow(tk.Toplevel):
""" displays progress """
def __init__(self):
super().__init__()
self.progress = ttk.Progressbar(
self, orient="horizontal", length=300, mode="indeterminate")
self.progress.pack()
self.note = "Processing data..."
self.p_label = tk.Label(self, text=self.note)
self.p_label.pack()
self.progress.start(50)
class FunctionClass:
""" thread1 works on this class """
def __init__(self, user_data):
self.user_data = user_data
self.do_something_else()
def do_something_else(self):
# simulate thread 1 working
time.sleep(3)
print("Thread1 job done")
if __name__ == "__main__":
root = tk.Tk()
Application(root)
root.mainloop()
* UPDATE *
tkinter's event_generate as suggested by iCart works well. See code update below.
import tkinter as tk
from tkinter import ttk, messagebox
from threading import Thread
import time
class Application:
def __init__(self, master):
# set main window
frame = tk.Frame(master, width=300, height=100)
frame.pack(fill=tk.BOTH)
# button widget
run_button = tk.Button(frame, text="GO", command=self.do_something)
run_button.pack()
# simulate some gui input from user
self.user_input = "specified by user"
def do_something(self):
thread1 = Thread(target=FunctionClass, args=(self.user_input,))
thread1.start() # launch thread
ProgressWindow() # main thread continues to new tkinter window
class ProgressWindow(tk.Toplevel):
""" displays progress """
def __init__(self):
super().__init__()
self.progress = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate")
self.progress.pack()
self.note = "Processing data..."
self.p_label = tk.Label(self, text=self.note)
self.p_label.pack()
self.progress.start(50)
class FunctionClass:
""" thread1 works on this class """
def __init__(self, user_data):
self.user_data = user_data
self.do_something_else()
def do_something_else(self):
# simulate thread 1 working
time.sleep(3)
print("Thread1 job done")
# call <<stop>> virtual event (unsure if 'tail' is necessary here)
root.event_generate("<<stop>>", when="tail")
def end_program(*args):
""" called with tkinter event_generate command after thread completion """
messagebox.showinfo("Complete", "Processing Complete")
root.destroy()
if __name__ == "__main__":
root = tk.Tk()
Application(root)
root.bind("<<stop>>", end_program) # bind to event
root.mainloop()
I'm not too familiar with tkinter so i can't show you ze code, but you should look at tkinter's event system, particularly firing custom events.
This question may help you get started
You can use Queue, from Queue module.
def get_queue(self, queue):
status = queue.get()
if status:
self.do_your_action()
self.master.after(1000, self.get_queue)
And in the progress bar window, You pass queue and put something in it when progress bar finishes.

Getting data from a long function to update a GUI

I've got a GUI which gives the user the option to run a selection of tests.
These tests run in threads as they use MIDI data in.
I have a checkQueue() function that is run using after() but once the user selects a test, the checkQueue() function is no longer called until the function finishes.
How do I get the checkQueue to continue running during my buttonTest() function so I can use data from the test to update the GUI?
Here is a simplified version of my code:
import Tkinter as tk
import Queue
class Program(tk.Frame):
def __init__(self,parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initUI()
self.q = Queue.Queue()
self.after(200, checkQueue, self.q, self)
def initUI(self):
start = tk.Button(self.parent, command=self.runTest, text="Run Test 1")
start.pack()
self.instruction = tk.Label(self.parent, text="Press Button 1")
self.instruction.pack()
def runTest(self):
buttonTest(self)
def checkQueue(q,app):
print "Calling checkQueue"
while not q.empty():
#HandleData (update a label/canvas etc.)
app.update()
app.after(200, checkQueue,q,app)
def buttonTest(gui):
#Does lots of functions but is just a while for this example
x=1
while x==1:
if x == 100:
gui.q.put("Some Data")
def main():
root = tk.Tk()
root.configure(background="black")
app = Program(root)
root.mainloop()
root.destroy()
if __name__ == "__main__":
main()
I'm assuming buttonTest is the function that's calling your tests that are running in other threads. Even though the actual work is done in child threads, buttonTest is still running in the main thread, and its while loop is hogging all the main thread's processing cycles. Try starting buttonTest itself in its own thread. This will give the main thread the breathing room necessary to process your checkQueue calls.
def runTest(self):
Thread(target=buttonTest, args=(self,)).start()

Categories

Resources