这是我第一次尝试编程,所以对我来说这是很新的东西,但是我从中获得了很多乐趣。
我一直在尝试许多不同的事情,并进行了大量的试验和错误,但无法克服这个问题。我正在为公司的实习项目制作带有GUI的Timelapse相机。截至目前,我已经设法通过带有按钮的条目来更新间隔按钮。但是,当我按停止按钮时,间隔从给定时间切换到1秒,并继续拍照。即使多次按停止,也不会中断线程。在按下quit之后,使用root.destroy,它也会继续进行。我在做什么错了?
我尝试添加几行切换,尝试了一些Google搜索,但是我对python的了解还不足以了解我能找到的与我的项目或情况非常相似的任何解决方案。
class Capture:
def __init__(self, waittime=30):
self.capture_stop = False
self.capture_waittime = waittime
self.thread = None
self.capture_running = False
def capture_worker(self):
self.capture_running = True
try:
with picamera.PiCamera() as camera:
camera.resolution = (1024, 768)
for filename in camera.capture_continuous('/home/pi/Time-lapse/project 1 -{timestamp:%H-%M-%S-%f}.jpg'):
i = 0
while i < self.capture_waittime:
time.sleep(0.1)
i += 0.1
if self.capture_stop:
break
finally:
self.capture_running = False
self.capture_stop = False
def start(self):
if not self.capture_running:
self.thread = threading.Thread(target=self.capture_worker)
self.capture_stop = False
self.thread.start()
def stop(self):
if self.capture_running:
self.capture_stop = True
def isRunning(self):
return self.capture_running
def setWaitTime(self, waittime):
self.capture_waittime = waittime
capture = Capture()
def startButton():
interval = float(var_2.get())
capture.setWaitTime(interval)
capture.start()
def stopButton():
capture.stop()
def setInterval():
global root
var_1.set("The current interval has been set to: " + var_2.get())
interval = float(var_2.get())
capture.setWaitTime(interval)
def quit():
global root
capture.stop()
root.destroy()
我希望当按下停止按钮时,线程会停止并且不会将间隔更新为1秒。