tkinter - Python Class Attribute Error 'Event' has no attribute -
i created code collaboration game few friends , making fun, , complete noob @ python. created class battle arena part of game:
from tkinter import * import random root = tk() class battle: def __init__(self, monhealth): self.distance = 10 self.center = 5 self.monhealth = monhealth def left1(self): if self.center < 20: self.distance += 1 distance = abs(self.distance) self.center += 1 center = abs(self.center) print (str(distance) + " meters" + " " + str(center) + " meters") def right1(self): if self.center > -20: self.distance -= 1 self.center -= 1 center = abs(self.center) distance = abs(self.distance) print (str(distance) + " meters" + " " + str(center) + " meters") def lunge1(self): hit = [0,1,2,3,4] random.shuffle(hit) if abs(self.distance) <= 1 , hit[0] >= 2: print ("strike") self.monhealth -= 10 if self.monhealth < 0: print ("you win") elif abs(self.distance) <= 1: print ("blocked") else: print ("miss") def block1(self): print ("blocked enemy") root.bind("<left>", left1) root.bind("<right>", right1) root.bind("<return>", lunge1) root.bind("<shift_l>", block1)
when run class, following error:
attributeerror: 'event' object has no attribute 'center'
how fix problem?
i read other explanations , solutions about/for attribute errors, 'str' objects.
as side note, if delete 'center' entirely, gives me same error monhealth. think screwed code, there way salvage it? thanks!
you're using class wrongly. have ti initialize first:
a=battle(45) # can use number want instead of 45
then have bind
follows:
root.bind("<left>", a.left1) root.bind("<right>", a.right1) root.bind("<return>", a.lunge1) root.bind("<shift_l>", a.block1)
you can decorate class's methods @classmethod
, won't make sense in case you'll still have initialize self.monhealth
variable.
Comments
Post a Comment