1 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
歡迎來到并發(fā)世界(有點(diǎn))!
試想一下,你的程序運(yùn)行:根據(jù)執(zhí)行流程,他們定義的指令正在執(zhí)行一個(gè)接一個(gè)的在它們的排列順序后,與例外的shutterPressed這是異步執(zhí)行的(可能)。
因此,假設(shè)我們進(jìn)入循環(huán)并在第一行<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!")
現(xiàn)在,shutterHasBeenPressed已設(shè)置為False并且驗(yàn)證了隨后的條件,以便我們輸入if.
程序一直運(yùn)行,直到意外地按下按鈕。說,它達(dá)到了<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!")
此時(shí),shutterPressed運(yùn)行,設(shè)置shutterHasBeenPressed為True。然后,回到我們的循環(huán),迭代結(jié)束,我們continue在循環(huán)的開始......那里有什么?!
shutterHasBeenPressed = False
按下按鈕完全沒有引起注意!
我相信這可以回答您的問題,詢問您做錯(cuò)了什么。
添加回答
舉報(bào)