'How to manage kivy file?

when we use kivy to create interface , the kivy file content of many lines of code, For example we have button with specific design, if I want use this button 10 times for each time should I write of properties of button ?

there is any way to manage the file? or we can make more than one kivy file to manage and easy understands code of kivy?



Solution 1:[1]

first define the class in the python file like

from kivymd.uix.behaviors import RoundedRectangularElevationBehavior, RectangularRippleBehavior
from kivy.uix.behaviors import ButtonBehavior
from kivymd.uix.boxlayout import MDBoxLayout


class CustomButon(RoundedRectangularElevationBehavior, RectangularRippleBehavior, ButtonBehaviour, MDBoxLayout):
    text = StringProperty()
    
    def fxn_to_call_on_release(self):
        pass

and your kv file should look somewhat like this when defining the properties of the button

<CustomButton>:
    size_hint: None, None 
    width: dp(80)
    height: dp(32)
    elevation: 20
    radius: [dp(15), dp(15), dp(15), dp(15)]
    canvas.before:
        Color:
            rgba: gch("#f3a635")
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [dp(15), dp(15), dp(15), dp(15)]
    
    MDLabel:
        text: root.text,
        halign: "center",
        font_style: "Button",
        opposite_colors: True,
        bold: True

The properties defined are my preferences, you can edit as you see fit. with these definitions you can call the button in other widgets;

CustomButton:
    on_release: self.fxn_to_call_on_release()
    text: "Button Text"

you can call the button like this as many times as you wish.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 darkmatter08