How can i get attribute from xpath?

Attribute Error in Pygame?

  • So here's the code: bif="bg.jpg" mif="lady.png" import pygame, sys from pygame.locals import * pygame.init() screen=pygame.display.set_mode((640,42... background=pygame.image.load(bif).conv... mouse_c=pygame.image.load(mif).convert... x,y=0,0 movex, movey=0,0 while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type==KEYDOWN: if event.key==K_LEFT: movex=-1 elif event.key==K_RIGHT: movex=+1 elif event.key==K_UP: movey=-1 elif event.key==K_DOWN: movey=+1 if event.type==KEYUP: if event.key==K_LEFT: movex=0 elif event.key==K_RIGHT: movex=0 elif event.key==K_UP: movey=0 elif event.key==K_DOWN: movey=0 x+=movex y+=movey screen.blit(background,(0,0)) screen.blit(mouse_c,(x,y)) pygame.display.update() And whenever I run, it, it always displays: Traceback (most recent call last): File "C:/Users/*****/Desktop/maria", line 25, in <module> elif event.key==K_RIGHT: AttributeError: 'Event' object has no attribute 'key' I don't know what's wrong. I tried rewriting it but it shows the same error. Any help? I'm really frustrated about it.

  • Answer:

    Could it be that your indentation is off, that the elif clause on line 25 is not actually a part of the if block on line 22? As far as I can tell, that would be the only way for the Event object not to have a key attribute at that point. Edit: Yep, that image confirms it. The indentation of your elif clauses is incorrect. It must be deeper than the if that checks the event type, otherwise it will not "belong" to that clause, but be executed if it fails. (Which automatically means that it doesn't have a "key" attribute.)

N at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Could it be that your indentation is off, that the elif clause on line 25 is not actually a part of the if block on line 22? As far as I can tell, that would be the only way for the Event object not to have a key attribute at that point. Edit: Yep, that image confirms it. The indentation of your elif clauses is incorrect. It must be deeper than the if that checks the event type, otherwise it will not "belong" to that clause, but be executed if it fails. (Which automatically means that it doesn't have a "key" attribute.)

Atli

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.