python - How to pass properties from one class to another in kivy -
in application, have multiple screens. each screen corresponds class. display property calculated in class (screen) on screen. below simplified example outline problem. in class wordcomprehension, numericproperty count_r incremented, every time button pressed. now, display result of calculation in scorescreen. suggestions. here .py:
import kivy kivy.app import app kivy.uix.boxlayout import boxlayout kivy.uix.floatlayout import floatlayout kivy.uix.label import label kivy.uix.button import button kivy.properties import objectproperty kivy.properties import numericproperty class appscreen(floatlayout): app = objectproperty(none) class mainmenu(appscreen): pass class scorescreen(appscreen): score = numericproperty(0) def get_score(self): wordcomp = wordcomprehension() self.score = wordcomp.count_r class wordcomprehension(appscreen): count_r = numericproperty(0) count_w = numericproperty(0) def do_something(self): self.count_r += 1 class interfaceapp(app): def build(self): self.screens = {} self.screens["wordcomp"] = wordcomprehension(app=self) self.screens["menu"] = mainmenu(app=self) self.screens["score"] = scorescreen(app=self) self.root = floatlayout() self.goto_screen("menu") return self.root def goto_screen(self, screen_name): self.root.clear_widgets() self.root.add_widget(self.screens[screen_name]) if __name__ == "__main__": interfaceapp().run()
and .kv:
#:kivy 1.8.0 <mainmenu>: boxlayout: orientation: 'vertical' label: text: "choose category" font_size: 30 button: text: 'word comprehension' on_press: root.app.goto_screen("wordcomp") button: text: 'highscore' on_press: root.app.goto_screen("score") disabled: false <scorescreen>: boxlayout: orientation: 'vertical' button: text: 'get score' on_press: root.get_score() label: text: "word comprehension right answers:" + str(root.score) button: text: 'main menu' on_release: root.app.goto_screen("menu") <wordcomprehension>: boxlayout: orientation: 'vertical' label: text: str(root.count_r) button: text: 'do something' on_release: root.do_something() button: text: 'menu' on_press: root.app.goto_screen("menu")
you can bind kivy properties 1 another, when 1 changes other. bind setter() function of class. (http://kivy.org/docs/api-kivy.event.html)
here need do:
1) create numericproperty in scorescreen count_r. 2) bind count_r numericproperty in wordcomprehension numericproperty in scorescreen : '''in class wordcomprehension ''' self.bind(count_r=self.score_screen.setter('count_r')
now trick need have reference scorescreen instance in wordcomprehension class. in example, assumed have reference in self.score_screen
. leave how want assign reference.
now whenever count_r changed in wordcomprehension, setter function (which setattr() ) gets called on 'count_r' property of scorescreen.
another thing: line wordcomp = wordcomprehension()
in get_score() not referencing same wordcomprehension instance want. creating new object of class wordcomprehension , referencing it's count_r it's default value.
edit1 not sure if want code make score property update whenever count_r property changed:
import kivy kivy.app import app kivy.uix.boxlayout import boxlayout kivy.uix.floatlayout import floatlayout kivy.uix.label import label kivy.uix.button import button kivy.properties import objectproperty kivy.properties import numericproperty class appscreen(floatlayout): app = objectproperty(none) class mainmenu(appscreen): pass class scorescreen(appscreen): count_r = numericproperty(0) score = numericproperty(0) # making new wordcomprehension object, , not have # correct value of score. no longer need binding properties # together. #def get_score(self): # wordcomp = wordcomprehension() # self.score = wordcomp.count_r class wordcomprehension(appscreen): count_r = numericproperty(0) count_w = numericproperty(0) def do_something(self): self.count_r += 1 class interfaceapp(app): def build(self): self.screens = {} self.screens["wordcomp"] = wordcomprehension(app=self) self.screens["menu"] = mainmenu(app=self) self.screens["score"] = scorescreen(app=self) self.root = floatlayout() self.goto_screen("menu") # bind 2 properties together. whenever count_r changes in wordcomp # screen, score property in score screen reflect changes. self.screens["wordcomp"].bind(count_r=self.screens["score"].setter('score')) return self.root def goto_screen(self, screen_name): self.root.clear_widgets() self.root.add_widget(self.screens[screen_name]) if __name__ == "__main__": interfaceapp().run()
Comments
Post a Comment