kv与另一个类Object链接的语言id作为ObjectProperty kivy

时间:2014-04-11 06:36:54

标签: python kivy

我正在尝试使用Clock中的更新方法从另一个类更新标签文本,但我无法理解为什么它没有正确更新标签。我在下面有一个示例代码:

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()

1 个答案:

答案 0 :(得分:3)

问题是你有像

这样的行
print ScoreBar().time_Label.text

这并没有告诉你有关现有ScoreBar的任何信息,它会创建一个新的ScoreBar并返回有关它的信息。

tama,您可以参考self.parent.children[1]访问您最初添加的那个,或设计另一种方式来访问该参考。