我正在尝试将 pygame 库与 Leap Motion 一起使用。我试图在我的窗口中显示 Leap Motion 中的一些数据,但是当我尝试从我的SampleListener
类中检索一个变量并将其提供给我的Main
方法时,我得到了这个错误:
AttributeError:“函数”对象没有属性“avg_pos”
我对类>方法>变量结构有什么误解吗?
这是我正在使用的代码:
import Leap, sys, pygame
#Window
windowWidth = 800
windowHeight = 600
#Leap Motion Settings
class SampleListener(Leap.Listener):
def on_init(self, controller):
print "Initialized"
def on_connect(self, controller):
print "Connected"
def on_disconnect(self, controller):
# Note: not dispatched when running in a debugger.
print "Disconnected"
def on_exit(self, controller):
print "Exited"
def on_frame(self, controller):
# Get the most recent frame and report some basic information
if not self.frame.hands.empty:
# Get the first hand
hand = self.frame.hands[0]
# Check if the hand has any fingers
fingers = hand.fingers
if not fingers.empty:
# Calculate the hand's average finger tip position
avg_pos = Leap.Vector()
for finger in fingers:
avg_pos += finger.tip_position
avg_pos /= len(fingers)
print "Hand has %d fingers, average finger tip position: %s" % (
len(fingers), avg_pos)
#Application Loop
def main():
pygame.init()
window = pygame.display.set_mode((windowWidth,windowHeight))
pygame.display.set_caption("TEST123")
#Surface
background = pygame.Surface(window.get_size())
background = background.convert()
background.fill ((50,0,80))
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("%s" % SampleListener.on_frame.len(fingers), 1, (255, 255, 255))
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos)
# Create a sample listener and controller
listener = SampleListener()
controller = Leap.Controller()
# Have the sample listener receive events from the controller
controller.add_listener(listener)
clock = pygame.time.Clock()
while True:
clock.tick(60)
#Handle Input Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Remove the sample listener when done
controller.remove_listener(listener)
window.blit(background, (0,0))
pygame.display.flip()
if __name__ == "__main__":
main()
编辑:这是完整的回溯错误-
Line 53, in main
text = font.render("%s" % SampleListener.on_frame.avg_pos, 1, (255, 255, 255))
AttributeError: 'function' object has no attribute 'avg_pos'
谢谢!