Reputation: 101
Is there a way to check or print a widgets id if you are not sure what id it has? I have a layout I made, its a simple BoxLayout where my Label has its id(its done in the kv file), than I use that Layout multiple times in a GridLayout. Now I have multiple Labels, but they technically have the same id or maybe kivy changes the id if you use it multiple times? Thats what I wanted to check by printining all their ids.
a3.py
from BareBones import Skeleton
menu = ['espresso', 'latte', 'capuccino', 'nescafe', 'Sahlep', 'Mahlep']
keys = []
class Double(BoxLayout):
pass
class NewLayout(GridLayout):
def set_text(self,text):
print(self.root.ids.Skeleton.ids['label_id'].text)
pass
class MainPage(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
iterator = 1
for i in range(len(menu)):
b = Button(size=(100,50))
self.ids[str(iterator)] = b
self.add_widget(b)
keys.append(str(iterator))
iterator = iterator+1
print(keys)
print(len(menu))
for x, y in zip(menu, keys):
self.ids[y].text = x
class RightSide(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation='vertical'
for i in range(len(menu)):
self.add_widget(Skeleton())
kv = Builder.load_file("b3.kv")
class MainApp(App):
def build(self):
return kv
MainApp().run()
b3.kv
NewLayout:
cols: 2
MainPage:
RightSide:
<MainPage>:
orientation: 'vertical'
<Skeleton>
b1: b1
b2: b2
label_id: label_id
Button:
id: b1
text:'+'
on_press: root.on_button_click_plus()
Label:
id: label_id
Button:
id: b2
text:'-'
on_press: root.on_button_click_minus()
BareBones.py
class Skeleton(BoxLayout):
count = 0
my_text = ''
drink_name = ''
def on_button_click_plus(self):
print("Button clicked")
self.count += 1
self.ids.label_id.text = str(self.count)
self.my_text = str(self.count)
return self.my_text
def on_button_click_minus(self):
print("Button clicked")
self.my_text = str(self.count)
self.ids.label_id.text = str(self.count)
if self.count > 0:
self.count -= 1
self.ids.label_id.text = str(self.count)
else:
pass
def trying(self):
pass
class PlusMinus(BoxLayout):
menu = ['espresso']
keys = []
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation='vertical'
menu = self.menu
self.add_widget(Skeleton())
So this basically looks like this:
What I basically want to do is have a button that collects all the labels texts (the numbers) So that I can pair them with the menu array to know how many of them were added to our basket. I also want to reset the labels however I dont know how I can get the texts that are so far deep in the code.
Upvotes: 0
Views: 453
Reputation: 38837
If you change the drink_name
to a StringProperty
in the Skeleton
class:
class Skeleton(BoxLayout):
count = 0
my_text = ''
drink_name = StringProperty('')
and assign a value to that property when each Skeleton
instance is created:
class RightSide(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation='vertical'
for i in range(len(menu)):
self.add_widget(Skeleton(drink_name=menu[i]))
Then you can get a tally of the number of drinks added with a method in the MainPage
class:
def tally(self):
rightside = kv.ids.rightside
for skel in rightside.children:
if isinstance(skel, Skeleton):
print(skel.drink_name, skel.ids.label_id.text)
Plus one other required change. In your kv
, add an id
for the RightSide
instance:
NewLayout:
cols: 2
MainPage:
RightSide:
id: rightside
Upvotes: 1
Reputation: 101
So this is the error Pycharm shows, the red highlighting under kv.
class MainPage(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
iterator = 1
for i in range(len(menu)):
b = Button(size=(100,50))
self.ids[str(iterator)] = b
self.add_widget(b)
keys.append(str(iterator))
iterator = iterator+1
print(keys)
print(len(menu))
for x, y in zip(menu, keys):
self.ids[y].text = x
def tally(self):
rightside = self.kv.ids.rightside
for skel in rightside.children:
if isinstance(skel, Skeleton):
kv = Builder.load_file("b3.kv")
print(skel.drink_name, skel.ids.label_id.text)
class RightSide(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation='vertical'
for i in range(len(menu)):
self.add_widget(Skeleton(drink_name=menu[i]))
kv = Builder.load_file("b3.kv")
class MainApp(App):
def build(self):
return kv
MainApp().run()
So I changed the Stringproperty in the Skeleton file and the code looks like that with the changes you told me to add. However nothing happens, how do I actually use the tally() method?
Upvotes: 0