I have a Keypad App that loads 9 buttons from a RecycleView
with Viewclass
set as Button
in a RecycleGridLayout
.
The main problem is that I cannot get an instance of the buttons when the buttons is added as a Viewclass
. But I can get an instance of a button if I add the buttons as widgets to a normal GridLayout
.
This is the RecycleView code from which I cannot get the instance of a button:
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()
From this code I can get the instance:
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()
How do I get the button objects' properties i.e. kivy.uix.button.Button object
from a RecycleView where the Viewclass is 'Button'?
cannot get an instance of the buttons when the buttons is added as a Viewclass.
The solution is as follow.
In the kv file, add the following on_press
event for Button
class.
<Button>:
on_press:
if self.text == '#': app.root.process_output()
else: app.root.get_input(self)
In the Python script, make the following modifications.
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