1

I am creating a GUI with Tkinter in Python for a serial device that controls two moving axis. I have setup a tkinter window with buttons, Entries,...

One crucial part of the project is to stop the movement of an axis when a button is clicked. Let's look at this example: I can measure the absolute position of one axis by sending a command via the serial port, yet this measuring process may take up to two minutes, thus my whole program (including the GUI) waits for the serial device to feedback the position. I need it to wait for the response, but my GUI should still be responsive to issue the stop move command. I started working with multithreading, yet with no success. It seems like the thread is not being stopped.

Here are the code snippets:

global cPMthread
cPMthread = threading.Thread(target = doMeasuring, args = (getPort(), axis, steps))
cPMthread.start()

This is being called when the button to start the measuring is pressed.

def doMeasuring(port, axis, *therest):
  global cPMthread
  cPMthread = threading.currentThread()
  if getattr(cPMthread, "do_run", True):
      currentPosition = Agilis.measureCurrentPosition(port, axis)

This starts the new thread.

def stopMove(port, axis):
  global cPMthread
  print(cPMthread, "trying to stop the process")
  cPMthread.do_run = False

And here I try to set the flag to stop the process.

Thanks for any advice

martineau
  • 119,623
  • 25
  • 170
  • 301
X_841
  • 191
  • 1
  • 14
  • ***"the thread is not being stopped."***: Don't redefine here: `cPMthread = threading.currentThread()`. This is usless: `if getattr(cPMthread, "do_run", True):`, will always evaluate to `True`. Is `Agilis.measureCurrentPosition` blocking? – stovfl Dec 10 '19 at 08:11
  • Thanks for the response. It does not stop the process though. What do you mean by blocking? – X_841 Dec 10 '19 at 08:34
  • ***"What do you mean by blocking?"***: Means, runs forever e.g. in a `while True:` loop.Read [close-a-thread-on-multithreading](https://stackoverflow.com/questions/43683257/python-close-a-thread-on-multithreading) – stovfl Dec 10 '19 at 08:37
  • Yes, it is in a `while` loop, but I need it to be executing the `while` loop but still listen to an end signal. – X_841 Dec 10 '19 at 08:46
  • Have you read the given link? To **break** a `while True:` your `if not .do_run:` have to be inside the loop so it can be checked regular. – stovfl Dec 10 '19 at 08:52
  • You need to put the `while` loop in a separate thread and then periodically check to see if it has detected the measurement is finished (via some shared variable). Tkinter has a universal widget [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) method that can be used to schedule a function to be called after a specified delay, so it can be used to do this. There are many examples of using it here on SO. – martineau Dec 10 '19 at 08:53
  • I have read the link, but I am not sure how to implement it, since this `Agilis.measureCurrentPosition(port, axis)` is basically a library method. How could I send a variable to it after calling it? Is this even possible, or will I have to move it into my main code? – X_841 Dec 10 '19 at 09:00
  • @X_841 ***"basically a library method"***: Are you the owner of the module `Agilis`? If Yes, extend the function `measureCurrentPosition` with a `StopEvent`. – stovfl Dec 10 '19 at 09:12
  • [This](https://stackoverflow.com/questions/18018033/how-to-stop-a-looping-thread-in-python) is what I got from my search and I have actually been trying it from the beginning but I cant get it to work. When pressing the button I would need to change the variable inside the method of my Agilis module (yes, I am the owner). Is there a possibility to do that? – X_841 Dec 10 '19 at 09:26
  • 1
    @X_841 ***"cant get it to work."***: Have you implemented this: `while getattr(t, "do_run", True):` inside `measureCurrentPosition`? – stovfl Dec 10 '19 at 09:57
  • 1
    After playing around a bit I finally have the statement in the right position and it seems to be working now. Thanks a lot for the help! – X_841 Dec 10 '19 at 10:48

0 Answers0