Python Forum

Full Version: How to bind a midi signal to tkinter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My program renders a score. The render() function is a big heavy function. Inside a thread I run a while loop that checks if a connected midi controller sends a note on message and if so it writes that note to the file and runs the heavy render() function inside the thread. The problem is that this works way slower resulting in a short blink on the screen every time you insert a note using the midi controller which is anoying.

The goal is to call the render function from the mainloop so it runs the heavy function in the main program / not inside a thread. Is this possible in a way?

I made a small tkinter example where we have a thread while loop. How can i call the render function inside the while loop in a way that it uses the tkinter mainloop()?
from tkinter import Tk
from time import sleep
from threading import Thread

root = Tk()

def render():
    print('render the score!')

def whileloopthread():
    while True:
        sleep(1)
        render() # how to call this function from the mainloop?

Thread(target=whileloopthread).start()

root.mainloop()
When I use root.bind() the function gets called in the mainloop()

I hope there is a simple answer :)
I don't see how running the render() function in a different thread makes it slower. Usually people don't want to run a time consuming function in the main thread because the GUI is frozen during the computation. The task of the mainloop() is to process events, not run heavy computations.

It seems to me there must be some other reason for the short blink on the screen, which won't be solved by running the function in the main thread.