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.

  • And try class root(Player, FloatLayout): or class root(Player, FloatLayout): root at init ' FloatLayout.__init__(self) Player.__init__(self) - gil9red
  • Painted at the launch stage - Pavel
  • Just crash without errors? Is it painted for both of my options? - gil9red
  • Yes, the application is not responding. I tried and so and so. In addition, I also tried super () - the result is the same, as far as I understood in root () it is generally better not to put anything. - Pavel
  • one
    In my opinion, you need to write self.ids.player1_hp.text = str (player1.hp) - Aleksey Osinny

1 answer 1

Here are some examples of how to use id:

 from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button from kivy.lang.builder import Builder from kivy.uix.label import Label Builder.load_string(''' <A>: Button: text:'test' on_press:root.testing() BoxLayout: id:a Label: text:'Первый' BoxLayout: id:b Label: text:'Второй' ''') class A(BoxLayout): def __init__(self,**kwargs): super().__init__(**kwargs) def testing(self): try: #вот сейчас произойдет печать всех id этого класса print('Все айдишники класса А') print(self.ids) #id должен быть оригинальным иначе при повторении он будет один #и изменения производимые по id будут применяться к последнему виджету #которому был присвоен не уникальный id print() #я могу обращаться по id и по детям, но по детям немного не правильно #например я могу достать лейбл бокса "а" по индексу 0 print('В боксе с айди "а" по индексу "0" есть лейбл с текстом:') print(self.ids.a.children[0].text) print() except: print('не получается(') class TestApp(App): def build(self): box=BoxLayout() box.add_widget(A()) return box TestApp().run()