API Reference¶
-
class
EventQueue(handler, widget=None, maxsize=0)[source]¶ This is a subclass of the standard library
queue.Queue.An
EventQueuefeeds any objects sent to it into a handler function that is called from the main Tk event loop.NOTE:
The constructor for
EventQueuemust be called from the main Tk thread.handlerA callable that will be called to handle a process. It is called as
handler(event)whereeventis a value that has previously beenput()to theEventQueue. Thehandleris called from the tkinter event loop and may interact with tkinter objects, however note that it is not passed thewidgetthat was passed to theEventQueueconstructor.widgetA tkinter widget, or
Noneto use the default root widget. This widget reference is used to create a Tk event handler; it doesn’t really have to be associated with the events that are to be generated or handled.maxsizeThe maximum size of the queue. If
maxsize <= 0the size is not limited.
TODO: talk about exceptions from handler, and how to feed events in.
An
EventQueueimplements the context manager protocol, which means it can be used like this:with EventQueue(handler=handle_event) as q: invoke_process_to_feed_events_to(q)
The context manager exit operation calls
drain()on the queue, so all events have been processed by the time thewithcompletes.-
put(event, block=True, timeout=None)[source]¶ Add an event to the queue.
blockIf
True(the default), then wait if the queue is full, otherwise raisequeue.Fullimmediately.timeoutIf
blockisTrue, specifies the maximum time to wait, in seconds, before raising :exc:queue.Fullif the queue remains full. A value ofNonemeans wait indefinitely. Ignored ifblockisFalse.
-
put_nowait(event)[source]¶ Add an event to the queue without waiting.
Either puts the event, or raises
queue.Fullimmediately.
-
class
EventGenerator(*, generator=None, handler=None, start=True, queue=None, widget=None, group=None, name=None, maxsize=0)[source]¶ An
EventGeneratoris a subclass ofthreading.Thread.The constructor creates and optionally starts a thread that fetches values from an iterable, and feeds each into a
EventQueuewhich in turn will make callbacks to a handler function, to process the events in the main Tk thread.generatorAn iterable that will provide the events to be processed.
It will be called from a new thread, and each value that it generates will be put into a queue.
queueThe queue into which to put the generated events.
If None is passed, a new
EventQueueis created, using thehandler,widgetandmaxsizeparameters - seeEventQueuefor descriptions of those.startA boolean that indicates whether the thread should be started.
The default is to start the thread immediately. This saves having to write an explicit call to
start().groupShould be
None.This is reserved for a future extension to
threading.Thread.nameA name for the thread.
If
Noneis passed, a name of the formEventGenerator-Nis used, whereNis an integer.
Most of the above parameters will rarely be needed. The most typical pattern is expected to be:
EventGenerator( generator=source_of_events, handler=handler_of_events, widget=my_toplevel_widget )
This creates a (notionally) unlimited sized
EventQueuewhich handles events put into it by callinghandler_of_eventsfrom an event handler associated withmy_toplevel_widget. The events are generated by a thread that callssource_of_eventsuntil it is exhausted.EventGeneratorimplements the context manager protocol. If used as a context manager, exiting the context implies callingdrain()and so exit is delayed until the generator is exhausted and the queue drained - i.e. until all events have been processed.-
start()¶ Starts the event collection thread (a loop that calls the
generatorspecified in the constructor).This call is unnecessary if
start=Truewas passed (or defaulted) to the constructor.
-
join(timeout=None)¶ Waits (for up to
timeoutseconds, or indefinitely iftimeout is None) for completion of the event generator. Note: does not drain the queue.