There are a couple of options.
Create your own event loop
Instead of calling root.mainloop()
, write your own loop that does whatever audio capture you need and then calls root.update()
to process Tkinter events.
This might need some careful tuning. You don't want to loose any audio, nor do you want the GUI to become unresponsive.
An similar alternative to this:
Integrate asyncio
in your tkinter
program
If you are familiar with the asyncio style, this might do very well. It also has support in the language.
Unfortunately asyncio
is still kind of a work in progress. So the examples you'll find might be out of date and even unusable.
Use a separate process for audio
Launch a multiprocessing.Process
to do the audio capture. Use a multiprocessing.Pipe
to send commands to the process and to receive audio data (using after
).
You should probably create this process before creating the root window.
The biggest plusses of this method are:
- it cannot interfere with the GUI process,
- it can be tested separately from the GUI,
- any audio processing can be done in the second process (or even a third process) without interfering with the GUI.
Using a separate thread for audio
Since audio involves I/O, this might be a good fit for threading.
It won't do if you also need to do CPU-intensive processing of the audio data. In CPython, only one thread at a time can be executing Python bytecode. So when the second thread crunching numbers, your GUI thread might be starved of run time.