2023.07.09
python -m pip install git+https://github.com/Gribouillis/managetkeventdata.git
This module 'managetkeventdata' offers the following features
-
Generate virtual events in tkinter carrying any Python object as client data (which tkinter cannot do natively).
-
Bind virtual events to event handler in order to receive these virtual events and their client data.
-
Create proxies of Python objects which methods can be called from any thread but are executed in tkinter's main thread. Two kinds of proxies can be created:
- 'mute' proxies which methods have no return values (like procedures)
- 'ordinary' proxies which methods have a return value (like functions) The advantage of mute proxies is that the calling thread doesn't have to wait for the return value when calling a method.
The virtual events can be generated in any thread, the event handlers are always executed by tkinter's main thread.
In particular, this allows threads to update widgets by defining event handlers that manipulate the widgets.
Usage example with event generation:
# create root widget
root = Tk()
# Define an event handler for the example
def handle_it(event):
print(event.widget)
print(event.data)
# Use the instance to bind a virtual event to the handler
bind(root, '<<test>>', handle_it)
# Later in code, generate virtual event with client data.
# The event generation can be done in another thread,
# The client data can be an arbitrary Python object.
...
event_generate(root, '<<test>>', ['a', ['b', 'c']]))
Usage example with object proxy:
class Spam:
def ham(self, foo, bar=''):
return bar + foo + bar
root = Tk()
pb = ProxyBuilder(root)
proxy = pb.proxy(Spam())
def work():
# method call in other thread
# actual execution of object method in main thread
s = proxy.ham('oof', bar='--') # returns '--oof--'
def start_work():
threading.Thread(target=work).start()
Button(root,text="Start Work",command=start_work).pack()
root.main_loop()