2015-05-12 20 views
9

Cosa voglio sapere?Come accedere a id/widget di classi diverse da un file kivy (.kv)?

  1. Se il pulsante con id: button_b (classe Get_Boys) viene rilasciato, quindi Etichetta con ID: label_g (Get_Girls classe) deve cambiare.
  2. Se viene premuto il pulsante con id: button_b (classe Get_Boys), l'etichetta con id: root_lbl (classe Get_People) deve essere modificata.
  3. Se viene rilasciato il pulsante con id: root_btn (classe Get_People), Etichetta con id: label_b (classe Get_Boys) deve essere modificato.

È spiegato (piccolo) nel collegamento this, ma non dal punto di vista del principiante.

Ho 2 file

  1. test.py
  2. dates_test.kv

test.py

class Get_People(BoxLayout): 
    pass 

class Get_Boys(BoxLayout): 
    pass 

class Get_Girls(BoxLayout): 
    pass 

class TestApp(App): 
    def build(self): 
     self.load_kv('dates_test.kv') 
     return Get_People() 

file di dates_test.kv

<Get_People>: 
    orientation: 'vertical' 
    Button: 
     name: root_btn 
     id: root_btn 
     text: "I am Root Button" 
     on_release: change_label_b 
    Label: 
     id: root_lbl 
     text: "I am Root Label" 
    Get_Boys: 
    Get_Girls: 

<Get_Boys>: 
    Button: 
     id: button_b 
     text: "Button for boys" 
     on_press: change_label_root 
     on_release: change_label_g 
    Label: 
     id: label_b 
     text: "Label for boys" 

<Get_Girls>: 
    Button: 
     id: button_g 
     text: "Button for girls" 
    Label: 
     id: label_g 
     text:"Label for girls" 

risposta

12

Bene !, sembra che io abbia trovato la risposta e mi piacerebbe condividerla.

Prima di tutto diamo "id" nel file date_test.kv. In modo che tu possa accedervi in ​​codice python o in file .kv.

<Get_People>: 
    stuff_p: root_lbl 
    ... 
    Get_Boys: 
     id: gb 
    Get_Girls: 
     id: gg 

<Get_Boys>: 
    stuff_b: label_b 

<Get_Girls>: 
    stuff_c: label_g 

si potrebbe chiedere che cosa è stuff_p, stuff_b e stuff_c ???

Sono ObjectProperty definiti nelle proprie classi. Le modifiche apportate in stuff_b nel tuo codice python apportano modifiche in label_b come hai collegato nel file kivy.

class Get_People(BoxLayout): 
    stuff_p = ObjectProperty(None) 
    ... 

class Get_Boys(BoxLayout): 
    stuff_b = ObjectProperty(None) 
    ... 

class Get_Girls(BoxLayout): 
    stuff_c = ObjectProperty(None) 
    ... 

Per Parte 1 e Parte 2

  1. Se pulsante con ID: (classe Get_Boys) button_b viene rilasciato, quindi Label con id: label_g (Get_Girls classe) deve cambiare .

  2. Se viene premuto il pulsante con id: button_b (classe Get_Boys), l'etichetta con id: root_lbl (classe Get_People) deve essere modificata.

Nella Get_Boys classe (test.py) definire tali metodi.

def change_girl(self): 

    self.parent.ids.gg.stuff_c.text = "Boys changed Girls!" 
    #self.stuff_b.text = "i changed myself!" 

def change_people(self): 
    self.parent.ids.root_lbl.text = "Boys changed people!" 

vediamo cosa è successo qui ...

self.parent.ids.gg.stuff_c.text = "Ragazzi cambiati Girls!"

  1. self.parent riferisce alla classe Get_Parent.
  2. .ids.gg riferisce id che abbiamo creato in precedenza per Get_Girls.
  3. .stuff_c è usato per riferirsi label_g (sopra) nella classe Get_Girls .
  4. .text viene utilizzato per modificare il testo nell'etichetta.

e nel file .kv

<Get_Boys>: 
    stuff_b: label_b 
    Button: 
     id: button_b 
     text: "button 1" 
     on_release: root.change_girl() 
     on_press: root. change_people() 

Per Parte 3

  1. Se Button con id: root_btn (classe Get_People) viene rilasciato, quindi etichettare con id: label_b (Get_Boys classe) deve cambiare .

nel Get_People classe (test.py) definire un metodo.

def rooted(self): 
    self.ids.gb.stuff_b.text = "people changed boys!" 

e nel file di .kv

Button: 
    id: root_btn 
    text: "I am Root" 
    on_release: root.rooted() 
+1

La ringrazio molto. Ho faticato a capire il linguaggio KV e questo esempio ha fatto fare clic su molte cose. – VectorVictor