kv Language id linked with another class Object as ObjectProperty kivy
Pergunta
I am trying to update a label text from another class using its update method in Clock but I couldn't understand why its not updating the label properly .I have a sample code below :
gui_v9 = '''
#:import Clock kivy.clock.Clock
<Level_1>:
on_enter: self.EnterLevel_1()
<ScoreBar>:
time_Label: timelabel
GridLayout:
rows: 4
cols: 1
size: root.size
#Space away from border
padding: 2
spacing: 10
canvas:
Color:
rgba: 204/255.0, 204/255.0, 0/255.0, 1
Rectangle:
# self here refers to the widget i.e FloatLayout
pos: self.pos
size: self.size
Button:
text: 'Score'
size_hint: .5, .5
Label:
text: "Level 1"
Label:
text: "Time :"
id: timelabel
Button:
text: 'Mute'
'''
class ScoreBar(Widget):
time_Label = ObjectProperty(None)
def __init__(self):
super(ScoreBar, self).__init__()
class Level_1(Screen,Widget):
def __init__(self, **kwargs):
super(Level_1, self).__init__(**kwargs)
self.layout = GridLayout(cols=2,spacing=(10),padding=10)
def EnterLevel_1(self):
print "Hi This is EnterLevel_1 . Level One Gui work area "
scoreBar = ScoreBar()
Field = tama(speed=3)
self.layout.add_widget(Field)
self.layout.add_widget(scoreBar)
self.add_widget(self.layout)
Clock.schedule_interval(Field.update, 10.0/100)
#Field
class tama(Widget):
def __init__(self, speed=1 ):
super(tama, self).__init__()
self.speed = speed
self.id = "Field"
self.size = (800,600)
self.Extra = 200
print ScoreBar().time_Label.text
def update(self,dt):
print ScoreBar().time_Label.text
ScoreBar().time_Label.text ="cdfdfd"
# Create the screen manager
Builder.load_string(gui_v9)
sm = ScreenManager()
sm.add_widget(Level_1(name='level_1'))
class MyJB(App):
def build(self):
return sm
if __name__ == '__main__':
MyJB().run()
Solução
The issue is that you have lines like
print ScoreBar().time_Label.text
This doesn't tell you anything about the existing ScoreBar, it makes a new one and returns information about that.
From the tama
, you could refer to self.parent.children[1]
to access the one you actually originally added, or devise another way to access the reference.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow