import random from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.widget import Widget from kivy.uix.floatlayout import FloatLayout class Player(): """ Player's stats and moves """ def __init__(self): self.hp = 250 self.damage = 10 self.crit = 15 self.mod = 2 def start_game(self, *args): #init players player1 = Player() player2 = Player() self.player1_hp.text = str(player1.hp) class root(FloatLayout, Player): pass class app(App): def build(self): self.title = "Knights duel rev 1.0" return root() if __name__ == "__main__": app().run() and .kv file
<root>: canvas: Rectangle: pos: self.center_x - 5, 0 size: 10, self.height Button: id: begin text: 'Start' pos_hint: {'x': 0.01, 'center_y': 0.94} color: .12, .66, .95, 1 background_color: (.53, .89, .38, 1) background_normal: "" size_hint: .1, .1 font_size: self.height - dp(25) on_press: root.start_game() Label: id: player1_hp text: '0' The problem is that when you press the button, it gives an error:
AttributeError: 'root' object has no attribute 'player1_hp' although my root () class is inherited from the Player () class. Or .kv does not know how to inherit (which I doubt), which in turn will force all the methods to be rewritten into the root () class, which will contradict the normal logic of the project.
I even made a separate method in root () and rebind a button to it - the error is the same.
class root(Player, FloatLayout):orclass root(Player, FloatLayout):rootatinit'FloatLayout.__init__(self)Player.__init__(self)- gil9red