首页 > 解决方案 > 设置标志后线程不会停止

问题描述

我正在用 Python 中的 Tkinter 为控制两个移动轴的串行设备创建一个 GUI。我已经设置了一个带有按钮、条目、...的 tkinter 窗口

该项目的一个关键部分是在单击按钮时停止轴的移动。我们来看这个例子:我可以通过串口发送命令来测量一个轴的绝对位置,但是这个测量过程可能需要两分钟,因此我的整个程序(包括 GUI)等待串口设备反馈位置。我需要它来等待响应,但我的 GUI 仍应响应发出停止移动命令。我开始使用多线程,但没有成功。似乎线程没有被停止。

以下是代码片段:

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

当按下开始测量的按钮时调用它。

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

这将启动新线程。

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

在这里,我尝试设置标志以停止该过程。

感谢您的任何建议

标签: pythonmultithreading

解决方案


推荐阅读