KivyMD Dialog not sizing over it's widgets

Viewed 294

After some days looking for an answer, I finally decided that it was time to ask some more experienced users ! Here is my problem : in the following piece of code (simplified version of the original code), when I open the Dialog by clicking on the button, the opened window doesn't have the right size, and so one part of the GridLayout is appearing outside this popup.

I anyone has an idea, thanks in advance !

from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout

from kivy.uix.label import Label

from kivy.uix.button import Button
from kivymd.uix.dialog import MDDialog

class AppClass(MDApp):
        
    def build_toolbar(self):
        button = Button(text="Press")
        button.bind(on_press=self.popup)
        
        return button
    
    
    def build(self):
        
        self.theme_cls.theme_style = "Dark"
        
        layout = BoxLayout(orientation='vertical')
        
        toolbar = self.build_toolbar()
        
        layout.add_widget(toolbar)
        
        return layout
    
    #==============================
    
    def popup(self, instance):
        print("called")
        panel = self.build_settings_panel()
        self.dialog = MDDialog(
            type="custom",
            title="Settings",
            content_cls=panel
        )
        
        self.dialog.open()
        
    def build_settings_panel(self):
        panel = GridLayout(cols=2, row_default_height=100)

        for i in range(4):
            panel.add_widget(Label(text="Number"))
            panel.add_widget(Label(text=str(i)))
        
        return panel

if __name__ == '__main__':
    AppClass().run()
2 Answers

It appears that the problem is the size of the panel. way to fix that is to just calculate that size and set it in the build_settings_panel():

def build_settings_panel(self):
    row_height = 100
    total_height = 0
    panel = GridLayout(cols=2, row_default_height=row_height)

    for i in range(4):
        panel.add_widget(Label(text="Number"))
        panel.add_widget(Label(text=str(i)))
        total_height +=row_height
    panel.height = total_height
    return panel

You can try:

row_default_height=9

In your code:

from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout

from kivy.uix.label import Label

from kivy.uix.button import Button
from kivymd.uix.dialog import MDDialog

class AppClassjbsidis(MDApp):
    def build_toolbar(self):
        button = Button(text="Press")
        button.bind(on_press=self.popup)
        return button
    
    def build(self):
        self.theme_cls.theme_style = "Dark"
        layout = BoxLayout(orientation='vertical')
        toolbar = self.build_toolbar()
        layout.add_widget(toolbar)
        return layout
    
    #==============================
    
    def popup(self, instance):
        print("called")
        panel = self.build_settings_panel()
        self.dialog = MDDialog(
            type="custom",
            title="[color=ffffff]Settings",
            content_cls=panel
        )
        
        self.dialog.open()
        
    def build_settings_panel(self):
        panel = GridLayout(cols=2, row_default_height=9)

        for i in range(4):
            panel.add_widget(Label(text="Number"))
            panel.add_widget(Label(text=str(i)))
        
        return panel

if __name__ == '__main__':
    AppClassjbsidis().run()

Pictures:

enter image description here

Related