Kivy-将标签文本绑定到变量(仅Python) [英] Kivy - Bind Label Text To Variable (Python Only)

查看:95
本文介绍了Kivy-将标签文本绑定到变量(仅Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试让自己的标签自动更新,我已经阅读了12个StackOverflow问题,但无济于事.

I have been trying to get my labels to auto-update themselves for quite a while now and I have read over a dozen StackOverflow questions but to no avail.

我有一个全局对象,该对象包含一个整数值,我希望使用一个小部件类中的标签来显示该整数值.

I have an global object that contains an integer value that I want to be displayed with a label inside of one of my widget classes.

小部件类如下:

class Battle(Widget):

    def __init__(self, **kwargs):
        super(Battle, self).__init__(**kwargs)

        #Enemy Stats
        self.enemyBar = BoxLayout(orientation="horizontal", size=(Window.width, Window.height/8), center_y = Window.height - self.height/2)
        self.enemyBar.add_widget(Label(text=enemy.name))

        #Enemy Health Label
        health_label = Label(text=str(enemy.health))
        self.enemyBar.add_widget(health_label)
        self.add_widget(self.enemyBar)


    def update_health(instance, value):
        health_label.text = str(enemy.health) #<-- Error happens here

    enemy.bind(health=update_health)

在程序中更改enemy.health的值时,我也希望我的标签也更改.我不想使用任何一种kivy语言,因为我更喜欢只有一个主python文件.

When the value of enemy.health is changed in the program, I want my label to change as well. I do not want to use any of the kivy language because I prefer having just one main python file.

使用实体类创建敌人对象.这是实体代码:

The enemy object is created with an entity class. Here is the entity code:

class entity(Widget):
    #entity creation
    health = NumericProperty() 

    def __init__(self, health):
        self.health = health

运行代码时,我按下一个按钮,该按钮调用一个会改变敌人健康状况的函数,然后出现错误消息:

When I run the code I press a button that calls a function that changes the enemy health and then I get an error:

全局名称'health_label'未定义

以某种方式,当调用update_health函数时,程序看不到init中创建的health_label变量.

Somehow, when the update_health function is called, the program doesn't see the health_label variable that was created in the init.

推荐答案

您需要使用bind方法,如下所示

You need to use the bind method, something like the following

health_label = Label(text=enemy.health)
self.enemyBar.add_widget(health_label)
def update_health(instance, value):
    health_label.text = str(enemy.health)
enemy.bind(health=update_health)

这只是我脑海中的一个基本示例,可以根据程序的结构使其更整洁.如果敌人.健康是字符串,则可以执行enemy.bind(health=health_label.setter('text')).

This is just a basic example off the top of my head, it can be made neater depending on the structure of your program. If enemy.health is a string, you can just do enemy.bind(health=health_label.setter('text')).

要使其正常工作,健康属性必须是kivy属性:

For this to work, the health attribute must be a kivy property:

class Enemy(Something):
    health = StringProperty('')

在您的代码中,敌人似乎是一个全局对象.很可能有更好的方法可以做到这一点.另外,我建议使用kv语言-将代码放在一个文件中并没有任何特别的价值(实际上,一旦变得平凡,这通常被认为是不好的做法),并且kv使很多事情变得更容易,而python却不那么容易不适合某些任务.

In your code it seems that enemy is a global object. There is quite likely a better way to do that. Also, I recommend using kv language - there is not really any special value in putting your code in one file (indeed, it's commonly considered bad practice once it gets non-trivial), and kv makes a lot of things easier where python just isn't suited to certain tasks.

这篇关于Kivy-将标签文本绑定到变量(仅Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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