[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Requesting a hand with a simple Death Counter



Hi,

in general the python error messages are quite useful and usually point you at the right location of the Problem.
Not always but most of the time and certainly in this case!
It says the line "events = OnKeyboardEvent.event.get()"
has an "AttributeError: 'function' object has no attribute 'event'"
so lets look at that line. you have an object "OnKeyboardEvent" what is that?
Oh you define it earlier  as a function. so maybe this is the 'function' object the error message is talking about.
what do you do with that function object. you try to access an attribute event by writing "OnKeyboardEvent.event"
do functions have an attribute event? nope. so there you go. exactly what the error message was talking about.
without looking further at your code you probably meant "pygame.event.get()"

hope that helps.
cheers,
Lorenz


On 28/10/14 07:30, PBRGamer wrote:
I'm trying to add in some pyHook in order to make it work in the background
actually.  This is as far as I've gotten. I believe I've messed up the def
or the actual function but I'm just slamming my head against a wall at this
point. Can only learn so much so fast. 5 bucks on it's a total mess and I
should start over. LOL

Ideas?

Error Message :

Traceback (most recent call last):
   File "C:/Python27/Deathcounterv3", line 34, in <module>
     events = OnKeyboardEvent.event.get()
AttributeError: 'function' object has no attribute 'event'

Code :

# Death Counter
import sys
import pygame
from pygame.locals import *
import pyHook

#PyGame creates the window
pygame.display.init()
BLACK = (0,0,0)
WIDTH = 320
HEIGHT = 260
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

windowSurface.fill(BLACK)

# create a keyboard hook
def OnKeyboardEvent(event):
	print 'MessageName:',event.MessageName
	print 'Message:',event.Message
	print 'Time:',event.Time
	print 'Window:',event.Window
	print 'WindowName:',event.WindowName
	print 'Ascii:', event.Ascii, chr(event.Ascii)
	print 'Key:', event.Key
	print 'KeyID:', event.KeyID
	print 'ScanCode:', event.ScanCode
	print 'Extended:', event.Extended
	print 'Injected:', event.Injected
	print 'Alt', event.Alt
	print 'Transition', event.Transition
	print '---'
	
while True:
     events = OnKeyboardEvent.event.get()
     for event in events:
         if event.type==KEYDOWN:
             if event.key == K_1:
                 with open("deathcounter.txt", "rt") as in_file:
                     deathcount = int(in_file.read()) #store an integer
                     deathcount = deathcount + 1
                 with open("deathcounter.txt", "wt") as out_file:
                     out_file.write(str(deathcount))
             if event.key == K_2:
                 deathcount = 0
                 with open("deathcounter.txt", "wt") as out_file:
                     out_file.write(str(deathcount))
             print deathcount


     if event.type == QUIT:
         pygame.quit()
         sys.exit()


# create a hook manager
hm = pyHook.HookManager()
# watch for all keyboard events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()

# initialize pygame and start the game loop
pygame.init()

while(1):
	pygame.event.pump()





--
View this message in context: http://pygame-users.25799.x6.nabble.com/Requesting-a-hand-with-a-simple-Death-Counter-tp1481p1486.html
Sent from the pygame-users mailing list archive at Nabble.com.