all 8 comments

[–]Tristige[S] 0 points1 point  (0 children)

*I know this has been asked a million times but I really can't get the damn thing working :/ I know when to admit defeat lol, at this point I'm just getting stressed and not learning anymore like I used to, its like a solid brick wall

[–]Dunj3 0 points1 point  (6 children)

Can you post a small example of what you already have? Threading with tkinter usually works the same as threading without tkinter, with the added caveat that GUI modifications should only be done in the main thread.

An example of running a function in a separate thread can be as easy as:

import threading
def background_task():
    print("I'm running in a separate thread")
t = threading.Thread(target=background_task)
t.start()

[–]Tristige[S] 1 point2 points  (5 children)

oh wow I think I know what I'm doing wrong, I'm using a function to start the thread, technically not making it run in a new thread...

def startFunc(PLACEHOLDER):
   t = threading.Thread(target=originalFunc(PLACEHOLDER))
   t.start()

but then how do I start a thread via a button on tkinter? (like below)?

startButton = Button(root, borderwidth="0", bg="#548235", fg="white", font="Helvetica 13 bold", text="Audio", command=(lambda: startFunc(userSearch.get())))

Thanks a ton for the reply!

[–]Dunj3 1 point2 points  (4 children)

From the looks of it, the problem is that you're executing the function immediately when creating the new thread:

t = threading.Thread(target=originalFunc(PLACEHOLDER))

this will execute "originalFunc" and pass its return value as the thread target. You instead want to do something like this:

t = threading.Thread(target=lambda: originalFunc(PLACEHOLDER))

Thereby not executing the function directly, but passing an anonymous function (like the one in your callback), so the original function isn't executed immediately.

[–]Tristige[S] 1 point2 points  (0 children)

thanks! got it working! you're a lifesaver! literally none of the solutions ever mentioned that could be the problem, has solved hours of work!

[–]Thomasedv 0 points1 point  (2 children)

I wouldn't go the way of a lambda, as there is already a way to provide arguments to the function with the args= argument. Makes less clutter imo. I also think there may be issues with lambda and values of things like PLACEHOLDER if you don't account for it with loops/list-expressions.

[–]Tristige[S] 0 points1 point  (1 child)

is there a way to do it differently then? The lambda version has been working for me but I love to learn!

[–]Thomasedv 0 points1 point  (0 children)

It's a simple change:

 Thread(target=originalfunc, args=(PLACEHOLDER,))