i have figured out how to add multiple labels to a grid in the python file but i'm struggling to add ids to those labels in the for loop. I tried the following but that doesn't seem to work. Please also guide me to access those labels using the ids. Any help or guidance is highly appreciated. Thank you
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
Builder.load_file("gridtable.kv")
class MyBox(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
pass
# for i in range(48):
# self.b = TextInput(multiline=False, font_size=dp(30))
# self.mygrid.add_widget(self.b)
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols=6
self.textinputs = {}
for i in range(48):
key = i+1
self.textinputs[key] = TextInput(multiline=False,font_size=dp(30),on_text=self.calc(key))
self.add_widget(self.textinputs[key])
def calc(self,key):
print(self.textinputs[key])
class MyApp(App):
def build(self):
return MyBox()
if __name__ == "__main__":
MyApp().run()
<MyBox>:
mygrid:my_grid
orientation: "vertical"
MyGrid:
id: my_grid
size_hint: 1,0.8
BoxLayout:
orientation: "horizontal"
size_hint: 1,0.2
BoxLayout:
orientation: "vertical"
Button:
text: "Expense Total:"
Button:
text: "Revenue Total:"
Button:
text: "Profit:"
font_size: 40
Traceback (most recent call last):
File "c:\Users\jaika\OneDrive\Desktop\python\lil_curry_project\gridtable.py", line 38, in <module>
MyApp().run()
print(self.textinputs[key])
KeyError: 1
I tried the following but that doesn't seem to work.
When something doesn't work you should post the details of how/why it fails. For instance, does it crash with a traceback?
Please also guide me to access those labels using the ids
The id
used by kivy is set only via kv language, it has specific behaviour local to the kv rule in which it is set.
Since you're creating the widgets in python you don't need to set an id, you can store them in a list or a dictionary or anything else you like. For instance, you could set self.textinputs = {}
then for each one self.textinputs[i] = TextInput(...); self.add_widget(self.textinputs[i])
.