1 回答
TA贡献1794条经验 获得超8个赞
欢迎来到并发世界(有点)!
试想一下,你的程序运行:根据执行流程,他们定义的指令正在执行一个接一个的在它们的排列顺序后,与例外的shutterPressed这是异步执行的(可能)。
因此,假设我们进入循环并在第一行<here>:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
现在,shutterHasBeenPressed已设置为False并且验证了随后的条件,以便我们输入if.
程序一直运行,直到意外地按下按钮。说,它达到了<here>:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
此时,shutterPressed运行,设置shutterHasBeenPressed为True。然后,回到我们的循环,迭代结束,我们continue在循环的开始......那里有什么?!
shutterHasBeenPressed = False
按下按钮完全没有引起注意!
我相信这可以回答您的问题,询问您做错了什么。
添加回答
举报
