python标签文本变量_Kivy-将标签文本绑定到变量(仅Python)

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.

The widget class looks like this:

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

enemy.bind(health=update_health)

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:

global name 'health_label' is not defined

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

解决方案

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)

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')).

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

class Enemy(Something):

health = StringProperty('')

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值