如何使用Viewclass作为按钮从RecycleView获取实例?

时间:2019-05-20 18:37:30

标签: python python-3.x kivy kivy-language

我有一个键盘应用程序,该应用程序从RecycleView中的Viewclass设置为Button的{​​{1}}加载9个按钮。 主要问题是,将按钮添加为RecycleGridLayout时无法获得按钮的实例。但是,如果将按钮作为小部件添加到普通的Viewclass中,则可以获得按钮的实例。

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

GridLayout

通过此代码,我可以获取实例:

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

如何从Viewclass为“按钮”的RecycleView中获取按钮对象的属性,即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()

1 个答案:

答案 0 :(得分:1)

问题

  

将按钮添加为按钮时无法获得按钮的实例   视图类。

解决方案

解决方法如下。

kv文件

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

摘要-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