3

I am trying to fetch the selection of a MDDropwdownMenu inside my kv file I am using a MDRaisedButton by the following code

 MDRaisedButton:
                            id: select_warning_image_Button
                            text: "Menu labels"                          
                            opposite_colors:    True
                            elevation_normal:    0
                            on_release: MDDropdownMenu(items=app.menu_labels, width_mult=4).open(self)

In my main.py (see below) I have a class called MainApp which inherits from App. Inside this class I have created the attribute variable menu_labels. I want to use the function change_variable to set the value of the variable VARIABLE to the value of the menu. But I cannot seem to be able to use self.change_variable(…). Is there a value to fire a function when a specific value in the dropdown is selected?

# main.py
import …


class MainApp(App):
    VARIABLE = ""

    menu_labels = [
        {"viewclass": "MDMenuItem",
         "text": "Label1",
         "on_release": self.change_variable("Label1")},
        {"viewclass": "MDMenuItem",
         "text": "Label2",
         "on_release": self.change_variable("Label2")},
    ]

    def change_variable(self, label):
        self.VARIABLE = label
MrYouMath
  • 547
  • 2
  • 13
  • 34

1 Answers1

5

Solution

In the output, Label2 was selected. Please refer to the snippet, example and output for details.

kv file

  1. Add a class rule for <MDMenuItem>:
  2. Add on_release event to invoke change_variable() method in App class and pass self.text to it.

Snippet - kv file

<MDMenuItem>:
    on_release: app.change_variable(self.text)

Example

main.py

from kivy.app import App
from kivymd.theming import ThemeManager


class MainApp(App):
    title = "KivyMD MDDropdownMenu Demo"
    theme_cls = ThemeManager()

    VARIABLE = ""

    menu_labels = [
        {"viewclass": "MDMenuItem",
         "text": "Label1"},
        {"viewclass": "MDMenuItem",
         "text": "Label2"},
    ]

    def change_variable(self, value):
        print("\nvalue=", value)
        self.VARIABLE = value
        print("\tself.VARIABLE=", self.VARIABLE)


if __name__ == "__main__":
    MainApp().run()

main.kv

#:kivy 1.11.0
#:import MDDropdownMenu kivymd.menu.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton

<MDMenuItem>:
    on_release: app.change_variable(self.text)

Screen:
    name: 'menu'
    MDRaisedButton:
        id: select_warning_image_Button
        size_hint: None, None
        size: 3 * dp(48), dp(48)
        text: 'Menu labels'
        opposite_colors: True
        elevation_normal:    0
        pos_hint: {'center_x': 0.1, 'center_y': 0.9}
        on_release: MDDropdownMenu(items=app.menu_labels, width_mult=4).open(self)

Output

Img01 - MDDropdownMenu opened Img02 - Selected Label2

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Really appreciate your help. I will try this solution and accept the answer if it works :). – MrYouMath Aug 15 '18 at 17:53
  • Thank you for your help @ikolim! Your contributions should be added to the official documentation :). – MrYouMath Aug 15 '18 at 20:21
  • @ikolim how to make it work if there are multiple dropdowns in the kv? how can we call a different function for each dropdown? – Philomath Mar 23 '20 at 10:11