constraints on programs for examplebecause spawned threads cannot usually perform gui processingthey must generally communicate with the main thread using global variables or shared mutable objects such as queuesas required by the application spawned thread which watches socket for datafor instancemight simply set global variables or append to shared queueswhich in turn triggers gui changes in the main thread' periodic after event callbacks the main thread' timer events process the spawned thread' results although some gui operations or toolkits may support multiple threads better than othersgui programs are generally best structured as main gui thread and non-gui "workerthreads this wayboth to avoid potential collisions and to finesse the thread safety issue altogether the pymailgui example later in the bookfor instancewill collect and dispatch callbacks produced by threads and stored on queue also remember that irrespective of thread safety of the gui itselfthreaded gui programs must follow the same principles of threaded programs in general--as we learned in such programs must still synchronize access to mutable state shared between threadsif it may be changed by threads running in parallel although producer/consumer thread model based upon queues can alleviate many thread issues for the gui itselfa program that spawns non-gui threads to update shared information used by the gui thread may still need to use thread locks to avoid concurrent update issues we'll explore gui threading in more detail in and we'll meet more realistic threaded gui programs in part ivespecially in ' pymailgui the latterfor instanceruns long-running tasks in threads to avoid blocking the guibut both restricts gui updates to the main thread and uses locks to prevent overlap of operations that may change shared caches using the after method of all the event tools in the preceding listthe after method may be the most interesting it allows scripts to schedule callback handler to be run at some time in the future though simple devicewe'll use this often in later examples in this book for instancein we'll meet clock program that uses after to wake up times per second and check for new timeand we'll see an image slideshow program that uses after to schedule the next photo display (see pyclock and pyviewto illustrate the basics of scheduled callbacksexample - does something bit different example - pp \gui\tour\alarm py flash and beep every second using after(callback loop from tkinter import class alarm(frame)time toolsthreadsand animation