<tfoot id='56BTb'></tfoot><legend id='56BTb'><style id='56BTb'><dir id='56BTb'><q id='56BTb'></q></dir></style></legend>
        <bdo id='56BTb'></bdo><ul id='56BTb'></ul>

      <i id='56BTb'><tr id='56BTb'><dt id='56BTb'><q id='56BTb'><span id='56BTb'><b id='56BTb'><form id='56BTb'><ins id='56BTb'></ins><ul id='56BTb'></ul><sub id='56BTb'></sub></form><legend id='56BTb'></legend><bdo id='56BTb'><pre id='56BTb'><center id='56BTb'></center></pre></bdo></b><th id='56BTb'></th></span></q></dt></tr></i><div id='56BTb'><tfoot id='56BTb'></tfoot><dl id='56BTb'><fieldset id='56BTb'></fieldset></dl></div>
    1. <small id='56BTb'></small><noframes id='56BTb'>

      1. Kivy - 将标签文本绑定到变量(仅限 Python)

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

          • <bdo id='9uc20'></bdo><ul id='9uc20'></ul>
              <tbody id='9uc20'></tbody>

            <small id='9uc20'></small><noframes id='9uc20'>

                • <legend id='9uc20'><style id='9uc20'><dir id='9uc20'><q id='9uc20'></q></dir></style></legend>
                  <i id='9uc20'><tr id='9uc20'><dt id='9uc20'><q id='9uc20'><span id='9uc20'><b id='9uc20'><form id='9uc20'><ins id='9uc20'></ins><ul id='9uc20'></ul><sub id='9uc20'></sub></form><legend id='9uc20'></legend><bdo id='9uc20'><pre id='9uc20'><center id='9uc20'></center></pre></bdo></b><th id='9uc20'></th></span></q></dt></tr></i><div id='9uc20'><tfoot id='9uc20'></tfoot><dl id='9uc20'><fieldset id='9uc20'></fieldset></dl></div>

                  <tfoot id='9uc20'></tfoot>
                  本文介绍了Kivy - 将标签文本绑定到变量(仅限 Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经尝试让我的标签自动更新很长一段时间了,我已经阅读了十几个 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.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')).

                  为此,health 属性必须是 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                  <legend id='tIfJF'><style id='tIfJF'><dir id='tIfJF'><q id='tIfJF'></q></dir></style></legend>
                  1. <small id='tIfJF'></small><noframes id='tIfJF'>

                        <bdo id='tIfJF'></bdo><ul id='tIfJF'></ul>
                      • <i id='tIfJF'><tr id='tIfJF'><dt id='tIfJF'><q id='tIfJF'><span id='tIfJF'><b id='tIfJF'><form id='tIfJF'><ins id='tIfJF'></ins><ul id='tIfJF'></ul><sub id='tIfJF'></sub></form><legend id='tIfJF'></legend><bdo id='tIfJF'><pre id='tIfJF'><center id='tIfJF'></center></pre></bdo></b><th id='tIfJF'></th></span></q></dt></tr></i><div id='tIfJF'><tfoot id='tIfJF'></tfoot><dl id='tIfJF'><fieldset id='tIfJF'></fieldset></dl></div>

                              <tbody id='tIfJF'></tbody>
                            <tfoot id='tIfJF'></tfoot>