KivyMD: Buttons don't work in custom MDDialog
Description of the Bug
The Buttons in the example for custom dialog don’t work. This is because it is not possible to define height: “120dp” in the KV-file. A workaround is to replace it with an integer.
This gets borked in dialog.py
if self.type == "custom":
if self.content_cls:
self.ids.container.remove_widget(self.ids.scroll)
self.ids.container.remove_widget(self.ids.text)
self.ids.spacer_top_box.add_widget(self.content_cls)
self._spacer_top = self.content_cls.height + dp(24) # <<<<------ here
self.ids.spacer_top_box.padding = (0, "24dp", "16dp", 0)
Code and Logs
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
KV = '''
<Content>
orientation: "vertical"
spacing: "12dp"
size_hint_y: None
height: "120dp"
MDTextField:
hint_text: "City"
MDTextField:
hint_text: "Street"
FloatLayout:
MDFlatButton:
text: "ALERT DIALOG"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.show_confirmation_dialog()
'''
class Content(BoxLayout):
pass
class Example(MDApp):
dialog = None
def build(self):
return Builder.load_string(KV)
def show_confirmation_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title="Address:",
type="custom",
content_cls=Content(),
buttons=[
MDFlatButton(
text="CANCEL", text_color=self.theme_cls.primary_color, on_release=self.callback
),
MDFlatButton(
text="OK", text_color=self.theme_cls.primary_color
),
],
)
self.dialog.open()
def callback(self, *args):
print('it works!')
Example().run()
Versions
- KivyMD: 0.104.1
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 15 (8 by maintainers)
ModalView has these functions:
When someone interacts with the dialog, it checks whether the touch was inside the dialog window, or outside. If it was outside, the dialog is dismissed. The problem is, that the actual size of MDDialog does not correspond to the size of ModalView. Therefore the dialog is dismissed even if the interaction was inside the window.
OK, so the issue is that MDDialog inherits from ModalView and ModalView does not update its size when widgets are added to it, or when children widgets change their size. Therefore, the collide_point property of MDDialog does not correspond to the real size of MDDialog. This can be fixed by updating the size of MDDialog at the end of initialisation.
@agisbrec The question is closed!