如何在 KivyMD 中制作下拉菜单,Python

How do I make a dropdown menu in KivyMD, Python

您好,我想知道如何制作下拉菜单,但我无法弄清楚我已经阅读了文档,但我不明白所以如果有人告诉我我尝试的问题在我的代码中,我将不胜感激.

PYTHON 文件:

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 文件:

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)

我不知道发生了什么事,所以如果有人知道答案并张贴在这里,我将不胜感激。

我不知道这个 class 是什么 - "MDMenuItem",你没有在你的例子中展示它,但据我了解你想在 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 中使用它。并且你不应该在示例中指定主题和颜色的定义,这不会影响任何东西。

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()