user interface - A Complete Code Example Of Kivy A Working Screen Manager Reference Written In KV Language -


i've been trying build kv language skills accessing id/widget of different class kivy file (.kv) using kivy's clock? working information found in kivy screen manager reference in kv language. unfortunately latter post not contain complete working code example can't understand how make changes text elements in on specific screen in multi-screen kivy application.

after searching of day can't find simple concrete working examples of how build multi-screen app in kv language here am. don't seem able set proper references individual screens can change them.

in simple example code below have built 4 screen application switches automatically between 4 screens. there 2 things i'd learn question;

  1. the kv language code set-up of screenmanager possible. ie. can lines 43 thru 47 reduced or eliminated kv language?

  2. the actual python code (which believe go on line 56 of app) changes text on first screen "hi i'm fifth screen" prior displayed second time.

code below. in advance. ....brad....

import kivy kivy.require('1.10.0') kivy.app import app kivy.lang import builder kivy.clock import clock kivy.uix.screenmanager import screenmanager, screen  builder.load_string(""" <firstscreen>:     name: '_first_screen_'     label:         id: first_screen         text: "hi i'm first screen" <secondscreen>:     name: '_second_screen_'     label:         id: second_screen         text: "hi i'm second screen" <thirdscreen>:     name: '_third_screen_'     label:         id: third_screen         text: "hi i'm third screen" <fourthscreen>:     name: '_fourth_screen_'     label:         id: fourth_screen         text: "hi i'm fourth screen" """)  class firstscreen(screen):     pass  class secondscreen(screen):     pass  class thirdscreen(screen):     pass  class fourthscreen(screen):     pass  sm = screenmanager() sm.add_widget(firstscreen()) sm.add_widget(secondscreen()) sm.add_widget(thirdscreen()) sm.add_widget(fourthscreen())  class switchingscreenapp(app):      def build(self):         clock.schedule_once(self.screen_switch_one, 2)         clock.schedule_once(self.screen_switch_two, 4)         clock.schedule_once(self.screen_switch_three, 6)         clock.schedule_once(self.screen_switch_four, 8)         # want place code here changes first_screen text "hi i'm fifth screen"         clock.schedule_once(self.screen_switch_one, 10)         return sm      def screen_switch_one(a,b):         sm.current = '_first_screen_'     def screen_switch_two(a,b):         sm.current = '_second_screen_'     def screen_switch_three(a,b):         sm.current = '_third_screen_'     def screen_switch_four(a,b):         sm.current = '_fourth_screen_'  switchingscreenapp().run() 

  1. yes can eliminate code creating root_widget , returning root widget in build method of app class , creating screens in kv file (i hope code explain litle bit better)

  2. to change text in python of label in kv file should use kivy properties , access widget of screen use manager.get_screen('screenname').widget command

here main.py file

    import kivy     kivy.require('1.10.0')     kivy.app import app     kivy.lang import builder     kivy.clock import clock     kivy.uix.screenmanager import screenmanager, screen     kivy.properties import objectproperty      class menuscreen(screen):         pass     class firstscreen(screen):         #create property in python         first_screen = objectproperty()         def starttimer(self):             self.timer = clock.schedule_once(self.screen_switch_two, 2)         def screen_switch_two(self, dt):             self.manager.current = 'second_screen'     class secondscreen(screen):         def starttimer(self):             self.timer = clock.schedule_once(self.screen_switch_three, 4)         def screen_switch_three(self, dt):             self.manager.current = 'third_screen'     class thirdscreen(screen):         def starttimer(self):             self.timer = clock.schedule_once(self.screen_switch_four, 6)         def screen_switch_four(self, dt):             self.manager.current = 'fourth_screen'     class fourthscreen(screen):         def starttimer(self):             self.timer = clock.schedule_once(self.screen_switch_one, 8)         def screen_switch_one(self, dt):             #change text of label in first_screen             self.manager.get_screen('first_screen').first_screen.text = "hi i'm fifth screen"             self.manager.current = 'first_screen'      root_widget = builder.load_string("""     #here screens created     screenmanager:         menuscreen:         firstscreen:         secondscreen:         thirdscreen:         fourthscreen:     #the menu screen created method on_enter in first screen called , loop through screens can begin     <menuscreen>:         button:             text: 'start'             on_press: app.root.current = 'first_screen'     <firstscreen>:         #link/bind property in kv file          first_screen: first_screen         name: 'first_screen'         on_enter: root.starttimer()         label:             id: first_screen             text: "hi i'm first screen"     <secondscreen>:         name: 'second_screen'         on_enter: root.starttimer()         label:             id: second_screen             text: "hi i'm second screen"     <thirdscreen>:         name: 'third_screen'         on_enter: root.starttimer()         label:             id: third_screen             text: "hi i'm third screen"     <fourthscreen>:         name: 'fourth_screen'         on_enter: root.starttimer()         label:             id: fourth_screen             text: "hi i'm fourth screen"     """)      class switchingscreenapp(app):          def build(self):             return root_widget      if __name__ == '__main__':         switchingscreenapp().run() 

to structure code better seperate kv , python code in 2 files:

main.py

import kivy kivy.require('1.10.0') kivy.app import app kivy.lang import builder kivy.clock import clock kivy.uix.screenmanager import screenmanager, screen kivy.properties import objectproperty  class menuscreen(screen):     pass class firstscreen(screen):     first_screen = objectproperty()     def starttimer(self):         self.timer = clock.schedule_once(self.screen_switch_two, 2)     def screen_switch_two(self, dt):         self.manager.current = 'second_screen' class secondscreen(screen):     def starttimer(self):         self.timer = clock.schedule_once(self.screen_switch_three, 4)     def screen_switch_three(self, dt):         self.manager.current = 'third_screen' class thirdscreen(screen):     def starttimer(self):         self.timer = clock.schedule_once(self.screen_switch_four, 6)     def screen_switch_four(self, dt):         self.manager.current = 'fourth_screen' class fourthscreen(screen):     def starttimer(self):         self.timer = clock.schedule_once(self.screen_switch_one, 8)     def screen_switch_one(self, dt):         self.manager.get_screen('first_screen').first_screen.text = "hi i'm fifth screen"         self.manager.current = 'first_screen'  class switchingscreenapp(app):      def build(self):         return builder.load_file('screen.kv')  if __name__ == '__main__':     switchingscreenapp().run() 

and screen.kv file

screenmanager:     menuscreen:     firstscreen:     secondscreen:     thirdscreen:     fourthscreen: <menuscreen>:     button:         text: 'start'         on_press: app.root.current = 'first_screen' <firstscreen>:     first_screen: first_screen     name: 'first_screen'     on_enter: root.starttimer()     label:         id: first_screen         text: "hi i'm first screen" <secondscreen>:     name: 'second_screen'     on_enter: root.starttimer()     label:         id: second_screen         text: "hi i'm second screen" <thirdscreen>:     name: 'third_screen'     on_enter: root.starttimer()     label:         id: third_screen         text: "hi i'm third screen" <fourthscreen>:     name: 'fourth_screen'     on_enter: root.starttimer()     label:         id: fourth_screen         text: "hi i'm fourth screen" 

i hope code you.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -