如何访问 FloatingActionButtonSpeedDial 中的 kivymd 1.1.1 按下的堆栈项目?

问题描述 投票:0回答:1

我想使用 KivyMDs FloatingActionButtonSpeedDial,根据按下的堆栈按钮会发生不同的情况。现在,我只是想打印按下了哪个按钮。我在文档中找不到任何示例,我找到的所有教程都说要使用

'''回调:回调'''

但是使用回调根本不会打印任何内容。我该如何解决这个问题?

from kivymd.app import MDApp
from kivy.lang.builder import Builder


class My_FirstApp(MDApp):

  data = {
      "Python": "language-python",
      "Ruby":"language-ruby",
      "JS":"language-javascript"
  }

  def callback(self, instance):

      print("press")

      print(instance.icon)

  def open(self):

      print("Open")

      self.root.ids.my_label.text = f'Open!'

  def close(self):

      print("CLose")

      self.root.ids.my_label.text = f'Close!'


  def build(self):

      self.theme_cls.theme_style = "Dark"

      self.theme_cls.primary_palette = "BlueGray"

      return Builder.load_file("SpeedDialButton.kv")


My_FirstApp().run()

##########################KV 文件######################### #########################

BoxLayout:
    orientation: "vertical"

    Screen:
            MDLabel:
                    id: my_label

                    text: "Stuff"

                    halign: "center"

            MDFloatingActionButtonSpeedDial:

                    data: app.data

                    root_button_anim: True

                    #PushTheButton

                    callback: app.callback

                    on_open: app.open()

                    on_close: app.close()
python kivy kivy-language kivymd
1个回答
0
投票

回调在 v1.1 中被删除,您现在可以向数据字典中添加更多数据,以调用单击的项目的 on_press 和 on_release 。 即。

self.data = {
    'Python': [
        'language-python',
        "on_press", lambda x: self.item_pressed('Python'),
        "on_release", lambda x: self.item_released('Python')
    ],
}

.kv 文件

MDFloatingActionButtonSpeedDial:
    data: app.data
    root_button_anim: True
    on_open: app.open()
    on_close: app.close()

这是您的代码已更新,可以使用新的方法。

快速拨号按钮.py

BoxLayout:
    orientation: "vertical"

    Screen:
        MDLabel:
            id: my_label
            text: "Stuff"
            halign: "center"

        MDFloatingActionButtonSpeedDial:
            data: app.data
            root_button_anim: True
            on_open: app.open()
            on_close: app.close()

主.py

from kivy.lang.builder import Builder
from kivy.properties import DictProperty
from kivymd.app import MDApp


class My_FirstApp(MDApp):
    data = DictProperty()

    def item_pressed(self, name) -> None:
        print(f'Button pressed: {name}')

    def item_released(self, name) -> None:
        print(f'Button released: {name}')

    def open(self):
        print("Open")

        self.root.ids.my_label.text = f'Open!'

    def close(self):
        print("CLose")

        self.root.ids.my_label.text = f'Close!'

    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "BlueGray"
        self.data = {
            'Python': [
                'language-python',
                "on_press", lambda x: self.item_pressed('Python'),
                "on_release", lambda x: self.item_released('Python')
            ],
            'Ruby': [
                'language-ruby',
                "on_press", lambda x: self.item_pressed('Ruby'),
                "on_release", lambda x: self.item_released('Ruby')
            ],
            'JS': [
                'language-javascript',
                "on_press", lambda x: self.item_pressed('JS'),
                "on_release", lambda x: self.item_released('JS')
            ],

        }
        return Builder.load_file("SpeedDialButton.kv")


My_FirstApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.