0

Hi I was wondering how to make a dropdown menu and I cant figure it out I have read the documentation but I do not understand so I would be grateful if someone told my the problem with my attempt at is in my code.

PYTHON FILE:

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton, MDRoundFlatButton
from kivymd.uix.textfield import MDTextField
from kivy.lang import Builder
from kivymd.uix.menu import MDDropdownMenu
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.uix.list import OneLineIconListItem


class IconListItem(OneLineIconListItem):
    icon = StringProperty()



class DemoApp(MDApp):
        
    def show_data(self):
        inputFahrenheit = self.root.ids.fahrenheit.text
        print(inputFahrenheit)
    
    def fahrenheitSelected(self):
        fahrenheit = True
        celsius = False 
    
    def on_start(self):
        self.theme_cls.primary_palette = "Green"
        self.theme_cls.primary_hue = "A700"
        self.theme_cls.theme_style = "Light"

        self.dropdown1 = MDDropdownMenu()

        self.dropdown1.items.append(

            {"viewclass": "MDMenuItem",
            "text": "option1",
            "callback": self.callback()}

        )

    def callback(self):
        print("cookies")

    def build(self):
        kv = Builder.load_file("test.kv") 
        
        screen = Screen()
            
        return kv
            
    
DemoApp().run()

KV FILE:

Screen:

    MDTextField:
        id: fahrenheit
        hint_text:"Enter Fahrenheit"
        helper_text: "Once you enter the fahrenheit the press submit"
        helper_text_mode: "on_focus"
        icon_right: "temperature-fahrenheit"
        pos_hint: {'center_x': 0.5, 'center_y': 0.9}
        size: 200, 25
        size_hint: None, None

            
    MDRoundFlatButton:
        text: "Enter"
        pos_hint: {'center_x': 0.5, 'center_y': 0.2}
        text_color: 0, 1, 0, 1
        size_hint: 0.25, 0.20
        on_release: app.show_data()

    MDIconButton:
        icon: "language-python"
        pos_hint: {"center_x": .5, "center_y": .5}
        on_release: app.dropdown.open(root)

I don't know what is going on hear so I would appreciate it if someone knew the answer and posted it here.

Ne1zvestnyj
  • 1,391
  • 1
  • 7
  • 25
GCIreland
  • 145
  • 1
  • 16

1 Answers1

0

I do not know what this class is - "MDMenuItem", you did not present it in your example, but as I understood you want to use it in the list menu, so I specified OneLineListItem as a viewclass, the list does not have a callback method (and in general it does not exist in almost all kivy classes), instead you should use on_release or on_press as in my example. If you do not want to use lambda, then you do not need to explicitly call the function - "on_release": self. callback. Also, to access some object of the class (in your example, it is MDDropdownMenu), it must be defined in __init__. Also, menu_items should be a list, not a dictionary as in your example, read the documentation carefully. You also forgot to specify the caller argument of the MDDropdownMenu class. I also noticed that you have the IconListItem class, but you don't use it anywhere, so I deleted it, if you want to create a list with icons, see this. And you should not specify the definition of the theme and color in the examples, this does not affect anything.

from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

from kivy.properties import StringProperty
from kivy.lang import Builder

KV = """
Screen:
    MDTextField:
        id: fahrenheit
        hint_text:"Enter Fahrenheit"
        helper_text: "Once you enter the fahrenheit the press submit"
        helper_text_mode: "on_focus"
        icon_right: "temperature-fahrenheit"
        pos_hint: {'center_x': 0.5, 'center_y': 0.9}
        size: 200, 25
        size_hint: None, None

    MDRoundFlatButton:
        text: "Enter"
        pos_hint: {'center': (0.5,0.2)}
        text_color: 0, 1, 0, 1
        size_hint: 0.25, 0.20
        on_release: app.show_data()

    MDIconButton:
        id: button
        icon: "language-python"
        pos_hint: {"center_x": .5, "center_y": .5}
        on_release: app.dropdown1.open()
"""


class DemoApp(MDApp):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)

        menu_items = [
            {
                "viewclass": "OneLineListItem",
                "text": "Option1",
                "on_release": lambda *args: self.callback()
            }
        ]

        self.dropdown1 = MDDropdownMenu(items=menu_items, width_mult=4, caller=self.screen.ids.button)

    def build(self):
        return self.screen

    def show_data(self):
        input_fahrenheit = self.root.ids.fahrenheit.text
        print(input_fahrenheit)

    @staticmethod
    def callback():
        print("cookies")


DemoApp().run()
Ne1zvestnyj
  • 1,391
  • 1
  • 7
  • 25