如何在 kv 中管理 kivy Spinner 小部件的值?

How to manage values for a kivy Spinner widget in kv?

我正在尝试实现一项功能,该功能会根据之前按下的按钮更改微调器值(下拉项)。但出于某种原因,任何直接在 kv 文件中编写 if 语句的尝试都会导致仅为值选择最新列表。

某些上下文 - 按三个客户端按钮之一会在 'collected_info' 字典 'Client' 键中添加一个值。然后,理想情况下,Spinner 小部件应该检查​​该键中的值是什么,并相应地更改它的(Spinner 的)值 属性。

这里是 python 代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import BooleanProperty
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder
from kivy.uix.widget import Widget


class ProtocolInfoPage(Screen):
    collected_info = {'Client': '', 'Work': '', 'Object': '',
                      'Pilot Drill': '', 'Extender': '', 'Reamer': '',
                      'Liners': {'4x20': [], '8x10': []}, 'Caula/Casing': ''}

    mup = ['1','2','3','4','5']

    olaf = ['6','7','8','9']

    fnb = ['10','10','10','10']

    def collect_info(self):
        self.collected_info['Pilot Drill'] = self.ids.pilot.text
        self.collected_info['Extender'] = self.ids.extender.text
        self.collected_info['Reamer'] = self.ids.reamer.text
        self.collected_info['Caula/Casing'] = self.ids.caula.text


kv = Builder.load_file("test.kv")


class MyApp(App):
    def build(self):
        return ProtocolInfoPage()


if __name__ == '__main__':
    MyApp().run()

这是 kv 文件:

<ProtocolInfoPage>:
    BoxLayout:
        orientation: 'vertical'

        GridLayout:
            cols:3
            size: 50,50
            ToggleButton:
                group: 'client'
                text: 'MUP'
                on_press: root.collected_info['Client'] = 'MUP'
            ToggleButton:
                group: 'client'
                text: 'Olaf Sitte'
                on_press: root.collected_info['Client'] = 'Olaf Sitte'
            ToggleButton:
                group: 'client'
                text: 'FNB'
                on_press: root.collected_info['Client'] = 'FNB'
        Spinner:
            id: object_dropdown
            size_hint: 1, None
            text: 'Choose Object'
            height: 44
            sync_height: True
            values: root.olaf
            on_text:
                root.collected_info['Object'] = object_dropdown.text

我知道在 python 代码中完成这些任务可能会更容易和更好,但我对 python 中声明的小部件插入已编写的方式有点困惑与其他小部件的 kv 布局。

如有任何帮助,我们将不胜感激!

您可以通过以下方式实现这一目标,

首先在你的 class ProtocolInfoPage 中定义一个方法,比如 update_collected_info as,

    def update_collected_info(self, text):
        self.collected_info['Client'] = text
        # Access the dropdown.
        dropdown = self.ids.object_dropdown
        # Now map separately or use a predefined dictionary for efficient access.
        if text == "MUP":
            dropdown.values = self.mup
        elif text == "Olaf Sitte":
            dropdown.values = self.olaf
        elif text == "FNB":
            dropdown.values = self.fnb
        print(self.collected_info)

现在从您 kvlang 中的每个按钮将 arg 'text' 传递给此方法作为,

        ...
        GridLayout:
            cols:3
            size: 50,50
            ToggleButton:
                group: 'client'
                text: 'MUP'
                # Assuming the value for the key "Client" will always be its text.
                # If necessary change and pass something you need.
                on_press: root.update_collected_info(self.text)
            ToggleButton:
                group: 'client'
                text: 'Olaf Sitte'
                on_press: root.update_collected_info(self.text)
            ToggleButton:
                group: 'client'
                text: 'FNB'
                on_press: root.update_collected_info(self.text)
        Spinner:
            id: object_dropdown
        ...