I’m trying to code put a KivyMD
MDIcon
and a MDLabel
inside a MDBoxLayout
. I want the width of the MDBoxLayout
to automatically increase or decrease as the size of the MDLabel
changes. But I’m experiencing a bit of a problem with the alignment of the label’s text.
Here’s my KivyMD code
MDScreenManager:
id: screen_manager
HomeScreen:
<HomeScreen>:
MDBottomNavigation:
id: bottom_nav
selected_color_background: "white"
text_color_active: "lightgrey"
MDBottomNavigationItem:
name: "home_page"
icon: "home"
text: "Home"
MDFloatLayout:
id: container
ScoreWidget:
size_hint: None, None
height: dp(50)
width: self.minimum_width
padding: [dp(10), dp(10), dp(10), dp(10)]
spacing: dp(10)
pos_hint: {"x": 0.6, "y": 0.8}
canvas.before:
Color:
rgba: .2, .6, .86, 1
RoundedRectangle:
pos: self.pos
size: self.size
radius: [dp(10),]
MDBoxLayout:
orientation: "horizontal"
size_hint: None, None
size: self.minimum_size
padding: dp(5)
spacing: dp(10)
MDIcon:
id: score_icon
icon: "star"
size_hint: None, None
size: dp(50), dp(50)
pos_hint: {"center_y": 0.5}
MDLabel:
id: score_label
text: "1280"
halign: "left"
size_hint: None, None
size: self.texture_size
pos_hint: {"center_y":
0.5}
And the Python code
class HomeScreen(MDScreen):
pass
class ScoreWidget(MDBoxLayout):
def __init__(self, **kwargs):
super(ScoreWidget, self).__init__(**kwargs)
self.orientation = "horizontal"
self.bind(minimum_width = self.setter("width"))
class Test(MDApp):
def build(self):
self.theme_cls.material_style = "M3"
self.theme_cls.theme_style = "Dark"
return Builder.load_string(kv)
if __name__ == '__main__':
Test().run()
Thank you