gpt4 book ai didi

python - 如何在事件回调之间保持 python 生成器的状态

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:20 25 4
gpt4 key购买 nike

所以我正在尝试学习 leap-motion SDK并在 4-5 年没有接触 python 之后重新学习它,我在 python 2.7 中遇到了生成器问题

基本上我有一个单词列表,每次跳跃 Action 选择一个新的“圆圈”手势时,我都想打印列表中的下一个单词。我所看到的是,每次 on_frame 回调触发时,只会打印列表中的第一个单词。我相信正在发生的事情是 python 运行时忘记了事件之间生成器的状态。有什么方法可以在手势事件之间保持生成器状态?

if not frame.hands.is_empty:
for gesture in frame.gestures():
if gesture.type == Leap.Gesture.TYPE_CIRCLE:
circle = CircleGesture(gesture)
# Determine clock direction using the angle between the pointable and the circle normal
action = None
if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
action = GestureActions.clockwiseCircleGesture()
else:
action = GestureActions.counterClockwiseCircleGesture()

print action.next()

def clockwiseCircleGesture():
words = ["You", "spin", "me", "right", "round", "baby", "right", "round", "like", "a", "record", "baby", "Right", "round", "round", "round", "You", "spin", "me", "right", "round", "baby", "Right", "round", "like", "a",
"record", "baby", "Right", "round", "round", "round"]
for word in words:
yield word

任何对此的见解都会很棒。谢谢

最佳答案

我怀疑每次触发事件时您的action 变量都会被重置。

在事件处理函数之外初始化生成器。看起来您可能需要两个,clockwise_actioncounterclockwise_action

关于python - 如何在事件回调之间保持 python 生成器的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com