-1

Im looking for a way to implement a function that would be running endlessly, while other functions, that would be called by pressing buttons, would not disrupt this functions flow and vice versa => 2 and possibly more functions running at the same time.

  • ***"mouseMove function executes first"***: The reason is using `time.sleep(1)`. Read [tkinter and time.sleep](https://stackoverflow.com/a/10393929/7414759) – stovfl Mar 01 '20 at 20:22
  • @stovfl remove time.sleep(1) and the function still dont run asynchronously. The mouseMove function still executes first – Jakub Jakubec Mar 01 '20 at 22:11
  • ***The mouseMove function still executes first"***: As it stands, there is no reason to do so, verified i get first `move()`. ***"still dont run asynchronously."***: How do you expect **async** using `lambda:`? [Edit] your question and explain in detail, using a [mcve], what you realy want to accomplish. – stovfl Mar 01 '20 at 22:43
  • @stovfl I will completly rephrase the question and the post itself so you can understand it better. – Jakub Jakubec Mar 01 '20 at 22:47
  • You want to [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Mar 01 '20 at 23:14
  • @stovfl i will look into that, thanks – Jakub Jakubec Mar 01 '20 at 23:16

1 Answers1

0

You can use the threading module to run something on a different thread so the main event loop won't freeze.

  1. Import the threading module
import threading
  1. Create a function you want the new thread to run

  2. Create a new thread and pass in the function you created

thread = threading.Thread(target=function, args=arguments)

Where target is the function you want to call in this new tread, and args are the arguments you need to pass into your function (you can leave args out if there are no arguments to pass in)

  1. Run the new thread
thread.start()
DYD
  • 382
  • 2
  • 15