使用Kivy的时钟从kivy文件(.kv)访问不同类的id/widget? [英] Accessing id/widget of different class from a kivy file (.kv) using Kivy's clock?

查看:123
本文介绍了使用Kivy的时钟从kivy文件(.kv)访问不同类的id/widget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在处理

I've spent the day working with the code from How to access id/widget of different class from a kivy file (.kv)? I've paired it down to the simplest code I could because what I want to do is use Kivy's Clock function to change the text for me instead of clicking the button.

据我所知,更改文本的代码应该在程序的第38行上执行,但是我尝试执行的所有代码都停止了执行,因为它无法访问更改文本.时钟功能正在提供的代码中起作用.

As best as I can understand the code that changes the text should go on Line 38 of the program but all the code I tried stopped executing because it could not access the text to change it. The clock function is working in the code provided.

我保持了按下按钮的活动状态,但是我想更改文本的是Clock代码.我想知道是否有人知道解决方案.

I've left the button press active but it's the Clock code that I want to change the text. I was wondering if someone knows the solution.

谢谢.

.... brad ....

....brad....

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

推荐答案

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

Please do the following and refer to my example for details:

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."

输出

这篇关于使用Kivy的时钟从kivy文件(.kv)访问不同类的id/widget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆