如何从视图类作为按钮的 RecyclerView 获取实例?

How to get the Instance from a RecycleView with Viewclass as button?

我有一个键盘应用程序,它从 RecycleView 加载 9 个按钮,ViewclassRecycleGridLayout 中设置为 Button。 主要问题是当按钮添加为 Viewclass 时,我无法获得按钮的实例。但是如果我将按钮作为小部件添加到普通 GridLayout.

,我可以获得按钮的实例

这是我无法从中获取按钮实例的 RecycleView 代码:

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty

Builder.load_string('''
<myKeyPad>:
    show_input: show_input
    TextInput:
        id: show_input
        hint_text: 'Type number'
        pos_hint: {'x': 0.05,'top': 0.98}
        size_hint: [0.9,0.1]
        font_size: self.height / 1.7
    RecycleView:
        id: view_keypad
        viewclass: 'Button'
        pos_hint: {'x': 0.35,'top': 0.86}
        size_hint: [1,1]
        RecycleGridLayout:
            cols: 3
            defualt_size: [None, 20]
            defualt_size_hint: [1, 0.2]

''')
class myKeyPad(FloatLayout):
    show_input = ObjectProperty(None)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        btn = ['7','8','9','4','5','6','1','2','3','*','0','#']
        key_padbtns=[]
        for b in btn:
            if b=='#':
                key_padbtns.append({'text':b, 'on_press':self.process_output})
            else:
                key_padbtns.append({'text':b, 'on_press':self.get_input})
        self.ids.view_keypad.data = key_padbtns
    def get_input(self,instance):
        the_input = (instance.text)
        i_no = self.show_input
        i_no.text = i_no.text + the_input
    def process_output(self):
        the_output = self.show_input.text
        self.show_input.text = ''
        print(the_output)
class theKeyPadApp(App):
    title='Keypad Input'
    def build(self):
        return myKeyPad()

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

从这段代码我可以得到实例:

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.properties import ObjectProperty

Builder.load_string('''
<myKeyPad>:
    show_input: show_input
    TextInput:
        id: show_input
        hint_text: 'Type number'
        pos_hint: {'x': 0.05,'top': 0.98}
        size_hint: [0.9,0.1]
        font_size: self.height / 1.7
    GridLayout:
        cols: 3
        id: view_keypad
        pos_hint: {'x': 0.05,'top': 0.86}
        size_hint: [0.9,0.5]

''')
class myKeyPad(FloatLayout):
    show_input = ObjectProperty(None)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        btn = ['7','8','9','4','5','6','1','2','3','*','0','#']
        print(btn)
        key_padbtns=[]
        for b in btn:
            if b=='#':
                key_padbtns.append(Button(text=b, on_press=self.process_output))
            else:
                key_padbtns.append(Button(text=b, on_press=self.get_input))

        for k in key_padbtns:
            self.ids.view_keypad.add_widget(k)
    def get_input(self,instance):
        print(instance)
        the_input = (instance.text)
        i_no = self.show_input
        i_no.text = i_no.text + the_input
    def process_output(self):
        the_output = self.show_input.text
        self.show_input.text = ''
        print(the_output)
class theKeyPadApp(App):
    title='Keypad Input'
    def build(self):
        return myKeyPad()

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

如何从视图类为 'Button' 的 RecycleView 中获取按钮对象的属性,即 kivy.uix.button.Button object

问题

cannot get an instance of the buttons when the buttons is added as a Viewclass.

解决方案

解决方法如下

kv 文件

在 kv 文件中,为 Button class 添加以下 on_press 事件。

片段 - kv 文件

<Button>:
    on_press:
        if self.text == '#': app.root.process_output()
        else: app.root.get_input(self)

Py文件

在Python脚本中,进行如下修改。

片段 - Py 文件

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    btn = ['7', '8', '9', '4', '5', '6', '1', '2', '3', '*', '0', '#']
    key_padbtns = []
    for b in btn:
        key_padbtns.append({'text': b})
    self.ids.view_keypad.data = key_padbtns