python - .kv file doesn't seem to work -


so i'm working example kivy file code, , came across code allows user switch between screens:

from kivy.app import app kivy.lang import builder kivy.uix.screenmanager import screenmanager, screen kivy.lang import builder kivy.uix.screenmanager import screenmanager, screen  builder.load_string(""" <menuscreen>:     boxlayout:         button:             text: 'build scenario'             on_press: root.manager.current = 'settings'         button:             text: 'run existing scenerio'  <settingsscreen>:     boxlayout:         button:             text: 'run existing scenerio'         button:             text: 'back menu'             on_press: root.manager.current = 'menu' """)  # declare both screens class menuscreen(screen):     pass  class settingsscreen(screen):     pass  # create screen manager sm = screenmanager() sm.add_widget(menuscreen(name='menu')) sm.add_widget(settingsscreen(name='settings'))  class testapp(app):      def build(self):         return sm  if __name__ == '__main__':     testapp().run() 

i wondered myself if possible put code given in builder.load_string() method separate .kv file. did that. commented builder part out (i admit don't know role is) , copied string .kv file looks this:

 # file name test.kv  #:kivy 1.0.9  <menuscreen>:     boxlayout:         button:             text: 'build scenario'             on_press: root.manager.current = 'settings'         button:             text: 'run existing scenerio'  <settingsscreen>:     boxlayout:         button:             text: 'run existing scenerio'         button:             text: 'back menu'             on_press: root.manager.current = 'menu' 

unfortunately when run code black screen. can tell me what's wrong? thanks!

the code creates screenmanager (sm) in main body of python file. when kv loaded file happens later, none of kv rules applied sm. okay before because load_string happens before instantiated.

for reason, instantiating widgets way bad practice, , sm = screenmanager(... etc should moved build method. run after kv file loaded, should work.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -