Kivy:如何在Python中引用kv ID? [英] Kivy: How to refernce kv ID in Python?

查看:154
本文介绍了Kivy:如何在Python中引用kv ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kivy的新手,我不得不认为这是可能的,但我无法弄清楚-按下按钮时如何更新Kivy标签,而只能在Python中引用该Kivy id ? (之所以尝试这样做,是因为在我的实际应用程序中,我希望一次更新几个标签,希望我可以在我应用程序中的button_pressed等价按钮内完成所有操作) .

I'm new to Kivy and I would have to think this is possible, but I can't figure it out - How can I update a Kivy label when a button is pressed, but only by referencing that Kivy id within Python? (The reason I'm trying to do it this way is because in my actual application, I would like several labels to update at once, which I was hoping I could do all within the button_pressed equivalent button I have in my app).

在下面的简单示例中,我只是试图按下按钮,然后将标签更新为已更新!"

In the simple example below, I'm just trying to have the button pressed and then have the label update to 'Updated!'

非常感谢!

我的Python代码:

My Python code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
import random


class TestingWidget(BoxLayout):

    # This is the kv id of the Label I would like to update
    label_to_update = StringProperty('')

    # This is the action I would like to happen when the button is pressed
    def button_pressed(self):
        label_to_update.text = 'Updated!'

class TestButtonApp(App):
    def build(self):
        return TestingWidget()

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

我的kv文件:

<TestingWidget>:
    BoxLayout:
        orientation: 'horizontal'
        Button: 
            text: 'test'
            on_press: root.button_pressed()
        Label:
            id: label_to_update
            text: 'Trying to get this to update'

推荐答案

当您按下按钮时,您肯定会更新所有标签.只需为每个对象创建一个StringProperty并执行您现在正在做的事情.

You definitely update all label when you press the button. Just crate a StringProperty for each and do what you are doing now.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from kivy.lang import Builder #used because I didn't want to create two files

import random

Builder.load_string('''
<TestingWidget>:
    BoxLayout:
        orientation: 'horizontal'
        Button: 
            text: 'test'
            on_press: root.button_pressed()
        Label:
            id: label_to_update
            text: root.label_to_update 
            ''')
class TestingWidget(BoxLayout):

    # This is the kv id of the Label I would like to update
    label_to_update = StringProperty('Trying to get this to update')
    #default text set
    # This is the action I would like to happen when the button is pressed
    def button_pressed(self):
        self.label_to_update = 'Updated!'

class TestButtonApp(App):
    def build(self):
        return TestingWidget()

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

这篇关于Kivy:如何在Python中引用kv ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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