How to put "ID" on MDDialog without use .kv structure

206 views
Skip to first unread message

berk berk

unread,
Feb 18, 2023, 9:46:06 AM2/18/23
to Kivy users support
Hey guys, I have a problem with MDDialog. Here is sample code:
....
if not self.dialog:
            self.dialog = MDDialog(
                title="Address:",
                type="custom",
                content_cls=MDBoxLayout(
                    MDTextField(
                        hint_text="City",
                    ),
                    orientation="vertical",
                    spacing="12dp",
                    size_hint_y=None,
                    height="120dp",
                ),
                buttons=[
                    MDFlatButton(
                        text="OK",
                        theme_text_color="Custom",
                        text_color=self.theme_cls.primary_color,
                    ),
                ],
            )
        self.dialog.open()
....
I want to put id on MDDialogs that its has TextField. But I couldn't. I now if I use .kv structure to create a content class like <Content> then i put on content_cls=Content() that's it. 
But how to put "id"  on normal Python structure like top sample?

Normally;
i=TextField(text="text")
self.ids['here']=i 
but I confused in this sample(MDDialog).

Thanks,

Elliot Garbus

unread,
Feb 18, 2023, 1:48:10 PM2/18/23
to kivy-...@googlegroups.com

The short answer is you don’t.

The id is a kv capability.  When the kv file is processed, the ids dict is populated.

You can address specific widgets or walk the widget tree.  I added some exampled below:

 

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.textfield import MDTextField


class Example(MDApp):
    dialog =
None

    def
build(self):
       
self.theme_cls.theme_style = "Dark"
       
self.theme_cls.primary_palette = "Orange"
       
return (
            MDFloatLayout(
                MDFlatButton(
                    
text="ALERT DIALOG",
                   
pos_hint={'center_x': 0.5, 'center_y': 0.5},
                   
on_release=self.show_confirmation_dialog,
               
)
            )
        )

   
def show_confirmation_dialog(self, *args):
       
if not self.dialog:
           
self.dialog = MDDialog(
               
title="Address:",
               
type="custom",
               
content_cls=MDBoxLayout(
                    MDTextField(
                       
hint_text="City",
                   
),
                   
MDTextField(
                       
hint_text="Street",
                   
),
                   
orientation="vertical",
                   
spacing="12dp",
                   
size_hint_y=None,
                   
height="120dp",
               
),
               
buttons=[
                    MDFlatButton(
                       
text="CANCEL",
                       
theme_text_color="Custom",
                       
text_color=self.theme_cls.primary_color,
                   
),
                   
MDFlatButton(
                       
text="OK",
                       
theme_text_color="Custom",
                       
text_color=self.theme_cls.primary_color,
                   
),
               
],
           
)
       
self.dialog.open()
       
print(f'{self.dialog.content_cls=}')
       
print(f'{self.dialog.content_cls.children}')
       
self.dialog.content_cls.children[0].text = '200 Main Street'
       
self.dialog.content_cls.children[1].text = 'Anytown'
       
print('-' * 80)
       
for w in self.dialog.walk(restrict=True):  # widgets under dialog
           
print(w)
       
print('-' * 80)
       
for w in self.dialog.walk(restrict=True):
           
if isinstance(w, MDTextField):
               
print(f'TextFiled: {w}', end=' ')
               
if w.hint_text == 'City':
                   
print('The City TextInput')
               
if w.hint_text == 'Street':
                   
print('The Street TextInput')



Example().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/a42c41c9-a5ff-4ca1-86df-6b2b03ec5d05n%40googlegroups.com.

 

berk berk

unread,
Feb 19, 2023, 5:25:45 AM2/19/23
to Kivy users support
Ohh "children"... Probably big problem is doesn't read full document :(
Thanks again +1

18 Şubat 2023 Cumartesi tarihinde saat 21:48:10 UTC+3 itibarıyla ElliotG şunları yazdı:
Reply all
Reply to author
Forward
0 new messages