How to capture the text from a selected row in an MDDialog

194 views
Skip to first unread message

Quantumrobs

unread,
Jan 18, 2022, 5:18:47 PM1/18/22
to Kivy users support
Hi everyone,

I have a quick question regarding MDDialog. I am using a "simple" dialog popup to show a list of names and their ID numbers from a search box entry. I would like to grab the user selected row so I can use the data for a database lookup.

I read a lot of documentation and cannot find the attribute for the on click event. I tried to guess at the attribute, but nothing I tried printed anything to the terminal when I tried them. I would like to know the binding syntax and the reference.

Any assistance would be helpful.

Thank you.


Robert

unread,
Jan 18, 2022, 7:26:58 PM1/18/22
to Kivy users support
The buttons have an on_release property not the dialog.
See the the example fragments here

Robert Sucarato

unread,
Jan 18, 2022, 7:51:50 PM1/18/22
to kivy-...@googlegroups.com, Kivy users support
Thank you for your reply. I did read over that information when I was trying to figure out where the values were being stored, but as you pointed out, there is nothing in regards to the row selection.

The rows do highlight when the user goes over them with the mouse, but I guess what you are saying is that it cannot have an event activated when the user clicks on a specific row?

If that is the case, does the checkbox in the confirmation type work in MDDialog? If I use the confirmation type and the user clicks a checkbox, will I be able to get the active state boolean value from that specific line item? If so, I will then be able to use it to set a variable on that particular record.

Thank you in advance for your assistance.

On Jan 18 2022, at 7:26 pm, Robert <planckp...@gmail.com> wrote:
The buttons have an on_release property not the dialog.
See the the example fragments here

Sent from Mailspring
On Tuesday, January 18, 2022 at 12:18:47 PM UTC-10 Quantumrobs wrote:
Hi everyone,

I have a quick question regarding MDDialog. I am using a "simple" dialog popup to show a list of names and their ID numbers from a search box entry. I would like to grab the user selected row so I can use the data for a database lookup.

I read a lot of documentation and cannot find the attribute for the on click event. I tried to guess at the attribute, but nothing I tried printed anything to the terminal when I tried them. I would like to know the binding syntax and the reference.

Any assistance would be helpful.

Thank you.



--
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.

Elliot Garbus

unread,
Jan 18, 2022, 8:58:56 PM1/18/22
to kivy-...@googlegroups.com

If you are using a search entry box (TextInput?) you could use an event like on_text or on_text_validate. 

If you want to add on_press and on_release event to a list item, you can create a new class derived from the ListItem and ButtonBehavior to add these button events to your line item object.  https://kivy.org/doc/master/api-kivy.uix.behaviors.button.html?highlight=buttonbehavior#module-kivy.uix.behaviors.button

 

If you share a minimal executable example I can provide more a more prescriptive answer.

--

Robert Sucarato

unread,
Jan 18, 2022, 10:39:50 PM1/18/22
to kivy-...@googlegroups.com, kivy-...@googlegroups.com
Thank you, Elliot.

The entry box is actually the search input box itself. The user's input would then be searched on the database to find the records that match (it is based on last name, so of course there can be more than one record; or the person entering the name may only know the first few letters, which could return numerous records as well).

The MDDialog would then pop up with the client's ID number, last name and first name. I have the dialog box popping up after the user presses the enter key; and the data is showing on the dialog window, along with the cancel button. The cancel function works fine with the current coding.

I have included a snippet of the Item class, along with the MDDialog definition. I have not done anything in the Kivy file, as I have the dialog window coded in the class containing the MDScreen. Obviously, I have not attached the database functionality to it yet, so the names are just hard-coded to see if this works correctly. The database linking is the easy part, once I know how to get the selected row data to use for the database SELECT.


class SearchParticipantItem(OneLineAvatarIconListItem):
    divider = None

class EditParticipantScreen1(MDScreen):
    dialog = None
    participant_search_dialog = None
 
    def user_change_rights(self):
        if LoginScreen.login_username == "Admin":
            self.manager.current = "userscreen"
        else:
            self.no_user_access_alert()
 
    def no_user_access_alert(self):
        if not self.dialog:
            self.dialog = MDDialog(
                title="User Change Rights Notice",
                text="You do not have the user rights to add, change or delete user credentials.",
                buttons=[
                    MDRectangleFlatButton(
                        text="OK", on_release=self.close_dialog),
                ],
            )
        self.dialog.open()
 
    def close_dialog(self, *args):
        self.dialog.dismiss()
 
    def participant_search_popup(self):
        if not self.participant_search_dialog:
            self.participant_search_dialog = MDDialog(
                title="Active Participant List",
                type = "simple",
                items=[
                    SearchParticipantItem(text = "888888    Washington, George"),
                    SearchParticipantItem(text = "777777    Jackson, Andrew"),
                ],
                buttons=[
                    MDRectangleFlatButton(
                        text="Cancel", on_release=self.close_participant_search_dialog),
                ],
                on_release = self.participant_search_dialog_selection()
            )
        self.participant_search_dialog.open()

Elliot Garbus

unread,
Jan 18, 2022, 10:54:38 PM1/18/22
to kivy-...@googlegroups.com, kivy-...@googlegroups.com

I’m assuming you want the user to select an item on the participant_serach_popup.

 

And the ButtonBehavior to the definition of the SearchParticipantItem.  Define the on_release action.  You can use the self.text as the parameter… in kv it might looks something like this.

 

<SearchParticipantItem>:

    on_release: self.look_up_item_in_database(self.text)

 

Read the ButtonBehavior example in the kivy docs, that uses a label.   You will do something similar, adding ButtonBehavior to your existing SearchParticpantItem class.

Robert Sucarato

unread,
Jan 18, 2022, 11:12:36 PM1/18/22
to kivy-...@googlegroups.com, kivy-...@googlegroups.com
Thank you for the assistance, Elliot.

So if I am understanding you correctly, the text output on the MDialog frame/window actually functions like a button even though it is text? Okay, so I understand how to retrieve self.text, so my last question is this:

This is my first time using Kivy (and based on how many things I am doing in this current application, I feel like I am learning a year's worth in a week). Anyway, I am a bit confused when it comes to putting non-screen manager items within the Kivy file. In this case, the SearchParticipantItem draws from OneLineAvatarIconListItem. Since this is not an MDScreen property, does it still get the same indentation in the Kivy file and added to the screen manager list inside the Kivy file?

Here is the way I have my .kv file set up. I did not include the entire thing since I think you get the picture from the snippet. I have the WindowManager with all the screens that I am currently using, and then each screen's attributes beneath it, starting with the LoginScreen.

Should I be putting the SearchParticipantItem in the file the same way, or does it go someplace else besides indenting under WindowManager?

Sorry for this additional questions, but I just want to make sure I am understanding correctly.


WindowManager:
    LoginScreen:
    MainScreen:
    UserScreen:
    AddParticipantScreen1:
    AddParticipantScreen2:
    AddParticipantScreen3:
    EditParticipantScreen1:
    EditParticipantScreen2:
    EditParticipantScreen3:
    DeleteParticipantScreen:
    AddStaffScreen1:
    AddStaffScreen2:
    EditStaffScreen1:
    EditStaffScreen2:
    DeleteStaffScreen:
    AddDeviceScreen:
    EditDeviceScreen:
    DeleteDeviceScreen:

<LoginScreen>:
    name: "loginscreen"
 
    MDFloatLayout:
        size: root.width, root.height
 
    MDLabel:
        text: "User Log In"
        pos_hint: {"center_y": 0.85}
        font_style: "H3"
        halign: "center"

Elliot Garbus

unread,
Jan 18, 2022, 11:43:21 PM1/18/22
to kivy-...@googlegroups.com, kivy-...@googlegroups.com

Here are few things to consider some of this is style…

I prefer to put the screen names under the class instances under the ScreenManager.  This will make a nice reference when you need to use the name to select a screen, all the screen names will be in the same place.

 

In KV writing the class name creates a class instance.  You can add the required attributes under the instance.  You have a number of classes that I suspect are very similar( AddParticipentScrren1, 2,3…) you may want to consider creating one customizable class and instancing that class multiple times, rather than creating 3 similar classes that will be error prone to maintain.

 

Putting those 2 ideas together…

 

 

WindowManager:

    LoginScreen:

        name: ‘login_screen’ 

    MainScreen:

        name: ‘main_screen’

    UserScreen:

        name: ‘user_screen’

    AddParticipantScreen:

        name: ‘aps_1’

        version: 1            # version could be a NumericProperty, that customizes the AddParticipantScreen Class.

    AddParticipantScreen:

        name: ‘aps_2’

        version: 2

    AddParticipantScreen:

        name: ‘aps_2’

        version: 3

         …

 

Should I be putting the SearchParticipantItem in the file the same way, or does it go someplace else besides indenting under WindowManager?

You can define a rule in kv, this is adding elements to the class definition.  The rule definition always starts on the far left column.

 

<SearchParticipantItem>:  # definition of a kivy rule

    on_release: print(f’{self.text} was pressed’)

 

When you put a class in kivy without the angle brackets, you are instancing the class.  Somewhere you are defining the screen (or a popup) that contains a list of SearchParticpantItems.  It will look something like this:

 

<AddParticipantScreen>:

    BoxLayout:

        orientation: ‘vertical’

        Label:

            text: ‘Important stuff below’

        MDList:

            id:mdlist

            SearchParticipantItem:

            SearchParticipantItem:

            SearchParticipantItem:

           

            

I added an id to show the list.  When kv is compiled, an ids dictionary is created that allows you to easily access the widgets from python by using the ids dictionary.

 

class AddParticipantScreen(Screen):

        def a_method(self):

            print(self.ids) # will show the dictionary that contains mdlist, and the instance of the MDLIst.

 

Hope that helps!

Robert Sucarato

unread,
Jan 19, 2022, 12:59:01 AM1/19/22
to Kivy users support
Thank you so much for this explanation. It really helps me to understand the Kivy file much better. That was a great explanation and I do appreciate the time you spent to explain it. Makes more sense now.

I will  also make the adjustments to the similar screens to keep things more organized and less error prone due to the redundancy.

Thank you again for the time and detail.

Elliot Garbus

unread,
Jan 19, 2022, 7:43:38 AM1/19/22
to kivy-...@googlegroups.com

It can some times be a challenge to find the relevant documentation.  This is a key reference for working with kv: https://kivy.org/doc/master/api-kivy.lang.html?highlight=lang#module-kivy.lang

Robert Sucarato

unread,
Jan 19, 2022, 5:34:48 PM1/19/22
to kivy-...@googlegroups.com
Hi Elliot,

I just wanted to let you know that your advice worked wonderfully. I am able to grab the text when I select the row.

Thank you again for taking the time to explain the Kivy file in detail. It makes working with it much easier. I know the Python side well. It was the way Kivy interacts and parses that had me confused. Your explanation should be included in the documentation because it was very easy to understand.

I appreciate it. You saved me a lot of hassle.





Elliot Garbus wrote:


You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/7nOzlqeGbP0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61e80776.1c69fb81.5ae46.3a3bSMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Elliot Garbus

unread,
Jan 19, 2022, 5:46:52 PM1/19/22
to kivy-...@googlegroups.com

Thanks for letting me know.  I’m glad to hear it worked out.

Reply all
Reply to author
Forward
0 new messages