I am using Kivy ScreenLayout and I want to know how to create a basic button which will appear in every Screen. For example:
<Scr1>: Button: pos: 500, 500 size: 200, 100 pos_hint: None, None text: "Button" #something else than in other screens <Scr2>: Button: pos: 500, 500 size: 200, 100 pos_hint: None, None text: "Button" #something else than in other screens <Scr3>: Button: pos: 500, 500 size: 200, 100 pos_hint: None, None text: "Button" #something else than in other screens
I don’t want to copy and paste the Button in every class, especially when I have more than 20 screens. Is there a way how can be the button added as a basic parameter to every class without copying? Thanks for any answer.
Advertisement
Answer
You can just use inheritance. Here is an example with two ways of using inheritance as you desire:
from kivy.app import App from kivy.factory import Factory from kivy.lang import Builder from kivy.uix.screenmanager import Screen, ScreenManager kv = ''' <BaseScreen>: Button: pos: 500, 500 size: 200, 100 size_hint: None, None text: "Button" on_release: app.root.current='scr2' <Scr1>: name: 'scr1' Label: text: 'Screen 1' <Scr2@BaseScreen>: # inheritance here name: 'scr2' Label: text: 'Screen 2' ''' class BaseScreen(Screen): pass class Scr1(BaseScreen): # inheritance here pass class TestApp(App): def build(self): Builder.load_string(kv) sm = ScreenManager() sm.add_widget(Scr1()) sm.add_widget(Factory.Scr2()) return sm TestApp().run()