【问题标题】:How to get the Instance from a RecycleView with Viewclass as button?如何以视图类作为按钮从 RecyclerView 获取实例?
【发布时间】:2019-05-21 06:16:16
【问题描述】:

我有一个键盘应用程序,它从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()

如何从 Viewclass 为“Button”的 RecycleView 获取按钮对象的属性,即 kivy.uix.button.Button object

【问题讨论】:

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


    【解决方案1】:

    问题

    当按钮被添加为 视图类。

    解决方案

    解决方法如下。

    kv 文件

    在 kv 文件中,为 Button 类添加以下 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
    

    【讨论】:

    • 解决了,非常感谢。只是一个问题,为什么在你的代码中你必须通过 App 实例访问根class myKeyPad
    • 使用app.root.xxx 访问根目录中实现的方法或属性,更快,更易于阅读和维护。使用self.parent.parent.parent.xxx 速度较慢,难以阅读和维护。当级别增加时,必须至少添加另一个parent
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 2013-07-19
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多