使用Kivy的时钟从kivy文件(.kv)访问不同类的id / widget?

时间:2017-09-10 00:08:02

标签: kivy kivy-language

我花了一天时间处理来自How to access id/widget of different class from a kivy file (.kv)?的代码我已将其配对到最简单的代码,因为我想要做的就是使用Kivy的时钟功能为我更改文本而不是单击按钮。

尽管我能理解更改文本的代码应该在程序的第38行,但我尝试的所有代码都停止执行,因为它无法访问文本来更改它。时钟功能在提供的代码中工作。

我已将按钮按下,但它是我要更改文字的时钟代码。我想知道是否有人知道解决方案。

提前致谢。

.... ....布拉德

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty

Builder.load_string("""
<SingleScreen>:
    id: single_screen
    orientation: 'vertical'
    Button:
        text: "You Can Change Me By Button Press Here But I Really Want To Be Changed By Kivy's Clock Function"
        on_release: root.rooted()
    Change_Label:
        id: gb
<Change_Label>:
    _python_access: ChangeLabel
    Label:
        id: ChangeLabel
        text: "I'm Gonna Change."
""")

class SingleScreen(BoxLayout):

    def rooted(self):
        self.ids.gb._python_access.text = "I Changed It By Clicking!. But That's Not What I Wanted To Program!"

class Change_Label(BoxLayout):
    _python_access = ObjectProperty(None)

class OnlyScreen(App):
    def build(self):
        Clock.schedule_interval(self.Callback_Clock, 3)
        return SingleScreen()

    def Callback_Clock(self, dt):
        print "Hello, world! I'm the clock working...."
        # What code goes here that will change the text via Kivy's clock instead of using the button?

if __name__ == '__main__':
    OnlyScreen().run()

2 个答案:

答案 0 :(得分:1)

请执行以下操作并参阅我的示例以获取详细信息:

片段

class OnlyScreenApp(App):

    def build(self):
        Clock.schedule_interval(self.Callback_Clock, 3)
        return SingleScreen()

    def Callback_Clock(self, dt):
        self.root.ids.gb._python_access.text = "I Changed It By Clock! dt=" + str(dt)

实施例

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty


class SingleScreen(BoxLayout):
    pass


class Change_Label(BoxLayout):
    _python_access = ObjectProperty(None)


class TestApp(App):

    def build(self):
        Clock.schedule_interval(self.Callback_Clock, 3)
        return SingleScreen()

    def Callback_Clock(self, dt):
        self.root.ids.gb._python_access.text = "I Changed It By Clock! dt=" + str(dt)


if __name__ == '__main__':
    TestApp().run()

test.kv

#:kivy 1.10.0

<SingleScreen>:
    id: single_screen
    orientation: 'vertical'
    Change_Label:
        id: gb

<Change_Label>:
    _python_access: ChangeLabel
    Label:
        id: ChangeLabel
        text: "I'm Gonna Change."

输出

enter image description here

答案 1 :(得分:0)

基于ikolim上面的回答,我想将解决方案发布为一个完整的python程序,该程序与发布问题的格式有关。真的很感激解决方案。完整的工作(一个文件)代码如下;

import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty

Builder.load_string("""
<SingleScreen>:
    id: single_screen
    orientation: 'vertical'
    Change_Label:
        id: gb

<Change_Label>:
    _python_access: ChangeLabel
    Label:
        id: ChangeLabel
        text: "I'm Gonna Change."
""")

class SingleScreen(BoxLayout):
    pass


class Change_Label(BoxLayout):
    _python_access = ObjectProperty(None)


class OneScreen(App):

    def build(self):
        Clock.schedule_interval(self.Callback_Clock, 3)
        return SingleScreen()

    def Callback_Clock(self, dt):
        self.root.ids.gb._python_access.text = "I Changed It By Clock! dt=" + str(dt)


if __name__ == '__main__':
    OneScreen().run()