How to make MDButton scale/fill the available space? Python3, KivyMD

I’ve tried searching Google and StackOverflow on how to do this already, even asked Gemini Code Assist how to achieve it, and it talks about the "size_hint" properties of the MDButton. I’ve tried using that and a few other similar properties but can’t seem to make my buttons scale to fill the available space within their MDBoxLayout.

Here is my current .kv file for the layout:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#:import MDLabel kivymd.uix.label.MDLabel
#:import MDBoxLayout kivymd.uix.boxlayout.MDBoxLayout
#:import MDButton kivymd.uix.button.MDButton
<MyRoot>:
random_label: random_label
MDBoxLayout:
orientation: "vertical"
MDLabel:
text: "Dice Roller"
font_style: "Display"
theme_text_color: "Custom"
text_color: 0.92, 0.45, 0, 1
halign: "center"
valign: "top"
size_hint_y: 0.33
size_hint_x: 1.0
MDLabel:
id: random_label
text: "-"
font_style: "Display"
role: "small"
halign: "center"
valign: "center"
size_hint_y: 0.33
size_hint_x: 1.0
MDBoxLayout:
orientation: "horizontal"
spacing: '10dp'
size_hint_y: 0.33
size_hint_x: 1.0
MDButton:
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_y: 1.0
size_hint_x: 0.2
on_press: root.roll_d2()
MDButtonText:
text: "Roll 1d2"
id: text
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_y: 1.0
size_hint_x: 0.2
on_press: root.roll_d4()
MDButtonText:
text: "Roll 1d4"
id: text
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_y: 1.0
size_hint_x: 0.2
on_press: root.roll_d6()
MDButtonText:
text: "Roll 1d6"
id: text
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_y: 1.0
size_hint_x: 0.2
on_press: root.roll_d8()
MDButtonText:
text: "Roll 1d8"
id: text
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_y: 1.0
size_hint_x: 0.2
on_press: root.roll_d10()
MDButtonText:
text: "Roll 1d10"
id: text
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
</code>
<code>#:import MDLabel kivymd.uix.label.MDLabel #:import MDBoxLayout kivymd.uix.boxlayout.MDBoxLayout #:import MDButton kivymd.uix.button.MDButton <MyRoot>: random_label: random_label MDBoxLayout: orientation: "vertical" MDLabel: text: "Dice Roller" font_style: "Display" theme_text_color: "Custom" text_color: 0.92, 0.45, 0, 1 halign: "center" valign: "top" size_hint_y: 0.33 size_hint_x: 1.0 MDLabel: id: random_label text: "-" font_style: "Display" role: "small" halign: "center" valign: "center" size_hint_y: 0.33 size_hint_x: 1.0 MDBoxLayout: orientation: "horizontal" spacing: '10dp' size_hint_y: 0.33 size_hint_x: 1.0 MDButton: style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" pos_hint: {"center_x": .5, "center_y": .5} size_hint_y: 1.0 size_hint_x: 0.2 on_press: root.roll_d2() MDButtonText: text: "Roll 1d2" id: text font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" pos_hint: {"center_x": .5, "center_y": .5} size_hint_y: 1.0 size_hint_x: 0.2 on_press: root.roll_d4() MDButtonText: text: "Roll 1d4" id: text font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" pos_hint: {"center_x": .5, "center_y": .5} size_hint_y: 1.0 size_hint_x: 0.2 on_press: root.roll_d6() MDButtonText: text: "Roll 1d6" id: text font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" pos_hint: {"center_x": .5, "center_y": .5} size_hint_y: 1.0 size_hint_x: 0.2 on_press: root.roll_d8() MDButtonText: text: "Roll 1d8" id: text font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" pos_hint: {"center_x": .5, "center_y": .5} size_hint_y: 1.0 size_hint_x: 0.2 on_press: root.roll_d10() MDButtonText: text: "Roll 1d10" id: text font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} </code>
#:import MDLabel kivymd.uix.label.MDLabel
#:import MDBoxLayout kivymd.uix.boxlayout.MDBoxLayout
#:import MDButton kivymd.uix.button.MDButton

<MyRoot>:
    random_label: random_label
    MDBoxLayout:
        orientation: "vertical"

        MDLabel:
            text: "Dice Roller"
            font_style: "Display"
            theme_text_color: "Custom"
            text_color: 0.92, 0.45, 0, 1
            halign: "center"
            valign: "top"
            size_hint_y: 0.33
            size_hint_x: 1.0

        MDLabel: 
            id: random_label
            text: "-"
            font_style: "Display"
            role: "small"
            halign: "center"
            valign: "center"
            size_hint_y: 0.33
            size_hint_x: 1.0
        
        MDBoxLayout:
            orientation: "horizontal"
            spacing: '10dp'
            size_hint_y: 0.33
            size_hint_x: 1.0
            
            MDButton:
                style: "elevated"
                theme_shadow_color: "Custom"
                shadow_color: "white"
                pos_hint: {"center_x": .5, "center_y": .5}
                size_hint_y: 1.0
                size_hint_x: 0.2
                on_press: root.roll_d2()

                MDButtonText:
                    text: "Roll 1d2"
                    id: text
                    font_style: "Title"
                    pos_hint: {"center_x": .5, "center_y": .5}

            MDButton:
                style: "elevated"
                theme_shadow_color: "Custom"
                shadow_color: "white"
                pos_hint: {"center_x": .5, "center_y": .5}
                size_hint_y: 1.0
                size_hint_x: 0.2
                on_press: root.roll_d4()

                MDButtonText:
                    text: "Roll 1d4"
                    id: text
                    font_style: "Title"
                    pos_hint: {"center_x": .5, "center_y": .5}

            MDButton:
                style: "elevated"
                theme_shadow_color: "Custom"
                shadow_color: "white"
                pos_hint: {"center_x": .5, "center_y": .5}
                size_hint_y: 1.0
                size_hint_x: 0.2
                on_press: root.roll_d6()

                MDButtonText:
                    text: "Roll 1d6"
                    id: text
                    font_style: "Title"
                    pos_hint: {"center_x": .5, "center_y": .5}

            MDButton:
                style: "elevated"
                theme_shadow_color: "Custom"
                shadow_color: "white"
                pos_hint: {"center_x": .5, "center_y": .5}
                size_hint_y: 1.0
                size_hint_x: 0.2
                on_press: root.roll_d8()

                MDButtonText:
                    text: "Roll 1d8"
                    id: text
                    font_style: "Title"
                    pos_hint: {"center_x": .5, "center_y": .5}

            MDButton:
                style: "elevated"
                theme_shadow_color: "Custom"
                shadow_color: "white"
                pos_hint: {"center_x": .5, "center_y": .5}
                size_hint_y: 1.0
                size_hint_x: 0.2
                on_press: root.roll_d10()

                MDButtonText:
                    text: "Roll 1d10"
                    id: text
                    font_style: "Title"
                    pos_hint: {"center_x": .5, "center_y": .5}

And my current main.py file for the application:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import numbergenerator
# Load the main-kvmd.kv file
Builder.load_file('main-kvmd.kv')
class MyRoot(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def roll_d2(self):
result = numbergenerator.Dice.roll_d2()
self.ids.random_label.text = str(result)
def roll_d4(self):
result = numbergenerator.Dice.roll_d4()
self.ids.random_label.text = str(result)
def roll_d6(self):
result = numbergenerator.Dice.roll_d6()
self.ids.random_label.text = str(result)
def roll_d8(self):
result = numbergenerator.Dice.roll_d8()
self.ids.random_label.text = str(result)
def roll_d10(self):
result = numbergenerator.Dice.roll_d10()
self.ids.random_label.text = str(result)
class DiceRoller(MDApp):
def build(self):
# Set the theme to dark
self.theme_cls.theme_style = "Dark"
sm = ScreenManager()
sm.add_widget(MyRoot(name='main'))
return sm
if __name__ == '__main__':
app = DiceRoller()
app.run()
</code>
<code>from kivymd.app import MDApp from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen import numbergenerator # Load the main-kvmd.kv file Builder.load_file('main-kvmd.kv') class MyRoot(Screen): def __init__(self, **kwargs): super().__init__(**kwargs) def roll_d2(self): result = numbergenerator.Dice.roll_d2() self.ids.random_label.text = str(result) def roll_d4(self): result = numbergenerator.Dice.roll_d4() self.ids.random_label.text = str(result) def roll_d6(self): result = numbergenerator.Dice.roll_d6() self.ids.random_label.text = str(result) def roll_d8(self): result = numbergenerator.Dice.roll_d8() self.ids.random_label.text = str(result) def roll_d10(self): result = numbergenerator.Dice.roll_d10() self.ids.random_label.text = str(result) class DiceRoller(MDApp): def build(self): # Set the theme to dark self.theme_cls.theme_style = "Dark" sm = ScreenManager() sm.add_widget(MyRoot(name='main')) return sm if __name__ == '__main__': app = DiceRoller() app.run() </code>
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import numbergenerator

# Load the main-kvmd.kv file
Builder.load_file('main-kvmd.kv')

class MyRoot(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def roll_d2(self):
        result = numbergenerator.Dice.roll_d2()
        self.ids.random_label.text = str(result)

    def roll_d4(self):
        result = numbergenerator.Dice.roll_d4()
        self.ids.random_label.text = str(result)

    def roll_d6(self):
        result = numbergenerator.Dice.roll_d6()
        self.ids.random_label.text = str(result)

    def roll_d8(self):
        result = numbergenerator.Dice.roll_d8()
        self.ids.random_label.text = str(result)

    def roll_d10(self):
        result = numbergenerator.Dice.roll_d10()
        self.ids.random_label.text = str(result)

class DiceRoller(MDApp):
    def build(self):
        # Set the theme to dark
        self.theme_cls.theme_style = "Dark"
        sm = ScreenManager()
        sm.add_widget(MyRoot(name='main'))
        return sm

if __name__ == '__main__':
    app = DiceRoller()
    app.run()

This resulted in this:

Here is my current simple Dice Roller application with 5 buttons at the bottom, encapsulated within a MDBoxLayout

I tried following an example from a similar question here, namely the use of MDGridLayout, but this didn’t seem to get me very far.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#:import MDLabel kivymd.uix.label.MDLabel
#:import MDBoxLayout kivymd.uix.boxlayout.MDBoxLayout
#:import MDButton kivymd.uix.button.MDButton
#:import MDGridLayout kivymd.uix.gridlayout.MDGridLayout
<MyRoot>:
random_label: random_label
MDGridLayout:
cols: 5
rows: 3
orientation: "lr-tb"
spacing: '5dp'
padding: '5dp'
size_hint: 1, 1
MDLabel:
text: "Dice Roller"
font_style: "Display"
theme_text_color: "Custom"
text_color: 0.92, 0.45, 0, 1
halign: "center"
valign: "center"
size_hint_y: 0.33
size_hint_x: 1.0
col_span: 5
row: 1
MDLabel:
id: random_label
text: "-"
font_style: "Display"
role: "small"
halign: "center"
valign: "center"
size_hint_y: 0.33
size_hint_x: 1.0
col_span: 5
row: 2
MDButton:
col: 1
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
size_hint_y: 0.33
on_press: root.roll_d2()
MDButtonText:
text: "Roll 1d2"
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
col: 2
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
size_hint_y: 0.33
on_press: root.roll_d4()
MDButtonText:
text: "Roll 1d4"
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
col: 3
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
size_hint_y: 0.33
on_press: root.roll_d6()
MDButtonText:
text: "Roll 1d6"
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
col: 4
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
size_hint_y: 0.33
on_press: root.roll_d8()
MDButtonText:
text: "Roll 1d8"
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
MDButton:
col: 5
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "white"
size_hint_y: 0.33
on_press: root.roll_d10()
MDButtonText:
text: "Roll 1d10"
font_style: "Title"
pos_hint: {"center_x": .5, "center_y": .5}
</code>
<code>#:import MDLabel kivymd.uix.label.MDLabel #:import MDBoxLayout kivymd.uix.boxlayout.MDBoxLayout #:import MDButton kivymd.uix.button.MDButton #:import MDGridLayout kivymd.uix.gridlayout.MDGridLayout <MyRoot>: random_label: random_label MDGridLayout: cols: 5 rows: 3 orientation: "lr-tb" spacing: '5dp' padding: '5dp' size_hint: 1, 1 MDLabel: text: "Dice Roller" font_style: "Display" theme_text_color: "Custom" text_color: 0.92, 0.45, 0, 1 halign: "center" valign: "center" size_hint_y: 0.33 size_hint_x: 1.0 col_span: 5 row: 1 MDLabel: id: random_label text: "-" font_style: "Display" role: "small" halign: "center" valign: "center" size_hint_y: 0.33 size_hint_x: 1.0 col_span: 5 row: 2 MDButton: col: 1 style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" size_hint_y: 0.33 on_press: root.roll_d2() MDButtonText: text: "Roll 1d2" font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: col: 2 style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" size_hint_y: 0.33 on_press: root.roll_d4() MDButtonText: text: "Roll 1d4" font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: col: 3 style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" size_hint_y: 0.33 on_press: root.roll_d6() MDButtonText: text: "Roll 1d6" font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: col: 4 style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" size_hint_y: 0.33 on_press: root.roll_d8() MDButtonText: text: "Roll 1d8" font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} MDButton: col: 5 style: "elevated" theme_shadow_color: "Custom" shadow_color: "white" size_hint_y: 0.33 on_press: root.roll_d10() MDButtonText: text: "Roll 1d10" font_style: "Title" pos_hint: {"center_x": .5, "center_y": .5} </code>
#:import MDLabel kivymd.uix.label.MDLabel
#:import MDBoxLayout kivymd.uix.boxlayout.MDBoxLayout
#:import MDButton kivymd.uix.button.MDButton
#:import MDGridLayout kivymd.uix.gridlayout.MDGridLayout

<MyRoot>:
    random_label: random_label
    MDGridLayout:
        cols: 5
        rows: 3
        orientation: "lr-tb"
        spacing: '5dp'
        padding: '5dp'
        size_hint: 1, 1

        MDLabel:
            text: "Dice Roller"
            font_style: "Display"
            theme_text_color: "Custom"
            text_color: 0.92, 0.45, 0, 1
            halign: "center"
            valign: "center"
            size_hint_y: 0.33
            size_hint_x: 1.0
            col_span: 5
            row: 1

        MDLabel:
            id: random_label
            text: "-"
            font_style: "Display"
            role: "small"
            halign: "center"
            valign: "center"
            size_hint_y: 0.33
            size_hint_x: 1.0
            col_span: 5
            row: 2

        MDButton:
            col: 1
            style: "elevated"
            theme_shadow_color: "Custom"
            shadow_color: "white"
            size_hint_y: 0.33
            on_press: root.roll_d2()

            MDButtonText:
                text: "Roll 1d2"
                font_style: "Title"
                pos_hint: {"center_x": .5, "center_y": .5}

        MDButton:
            col: 2
            style: "elevated"
            theme_shadow_color: "Custom"
            shadow_color: "white"
            size_hint_y: 0.33
            on_press: root.roll_d4()

            MDButtonText:
                text: "Roll 1d4"
                font_style: "Title"
                pos_hint: {"center_x": .5, "center_y": .5}

        MDButton:
            col: 3
            style: "elevated"
            theme_shadow_color: "Custom"
            shadow_color: "white"
            size_hint_y: 0.33
            on_press: root.roll_d6()

            MDButtonText:
                text: "Roll 1d6"
                font_style: "Title"
                pos_hint: {"center_x": .5, "center_y": .5}

        MDButton:
            col: 4
            style: "elevated"
            theme_shadow_color: "Custom"
            shadow_color: "white"
            size_hint_y: 0.33
            on_press: root.roll_d8()

            MDButtonText:
                text: "Roll 1d8"
                font_style: "Title"
                pos_hint: {"center_x": .5, "center_y": .5}

        MDButton:
            col: 5
            style: "elevated"
            theme_shadow_color: "Custom"
            shadow_color: "white"
            size_hint_y: 0.33
            on_press: root.roll_d10()

            MDButtonText:
                text: "Roll 1d10"
                font_style: "Title"
                pos_hint: {"center_x": .5, "center_y": .5}

Dice Roller app using MDGridLayout

All I want to achieve is to have the Title “Dice Roller” take up 33% height and 100% width at the top; the number text (“-“) take up 33% height and 100% width in the center, and make the buttons scale to take up 33% height of the bottom of the screen, and have them all resize appropriately next to each other, without overlapping, across the 100% of the width. (I.E: they don’t cut off any of the buttons/text.)

Sorry if this is a very basic question but I can’t figure out how to do it.

New contributor

Lunar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật