简体   繁体   English

kivy NumericProperty至StringProperty

[英]kivy NumericProperty to StringProperty

I tried to make my own Coockie Clicker in kivy, but with cristmas coockies. 我尝试用猕猴桃制作我自己的Coockie Clicker,但使用了cristmas coockies。 I created an Image of an Coockie, you can click on and an Label that shows you how many times you have clicked. 我创建了一个Coockie的图像,您可以单击,并显示一个标签,该标签显示您单击了多少次。 The Label required an String, so I tried to convert the numeric property into an string, but it did not work, because I got the error message: Label需要一个字符串,因此我尝试将numeric属性转换为字符串,但是它不起作用,因为出现了错误消息:

<kivy.properties.NumericProperty object at 0xa6e32cc>

This is the part of the code, where I suspect the error: 这是代码的一部分,我怀疑其中的错误:

class Kecks(Widget):

    count = NumericProperty(0)
    amount = NumericProperty(1)
    txt = StringProperty(str(count))

Here is the rest of the code: 这是其余的代码:

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.core.window import Window

from kivy.clock import Clock
from kivy.animation import Animation
from  kivy.core.text.markup import *

from kivy.uix.floatlayout import FloatLayout
from kivy.properties import NumericProperty
from kivy.properties import StringProperty

Builder.load_string('''
<Root>:

    Kecks:
        pos: 300, 300
<Kecks>:
    Image:
        pos: root.pos
        id: my_image
        source: 'piernik.png'

    Label:
        id: my_Label
        font_size: 50
        text: root.txt
        center_x: root.width / 4

''')

class Root(FloatLayout):
    pass

class Kecks(Widget):

    count = NumericProperty(0)
    amount = NumericProperty(1)
    txt = StringProperty(str(count))

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.count += self.amount
            print self.txt


class app(App):
    def build(self):
        Window.clearcolor = (10, 0, 0, 1)
        return Root()

if __name__ == "__main__":
    app().run()

That's not an error message, that's a string identifying a property instance. 这不是错误消息,而是标识属性实例的字符串。 Do you really mean you got an error, or is that what's printed by your program? 您真的是说您遇到了错误,还是程序打印了什么? I guess the latter, because... 我猜是后者,因为...

count = NumericProperty(0)
amount = NumericProperty(1)
txt = StringProperty(str(count)) 

To understand this, you need to know a little about how properties work - you declare them at the class level, not within a method, so they are attributes of the class (which are inherited as attributes of individual instances). 要了解这一点,您需要了解一些属性的工作原理-在级别(而不是在方法内)声明它们,因此它们是类的属性(继承为各个实例的属性)。 One effect of this is that all instances of the class share the same property objects. 这样的效果是该类的所有实例共享相同的属性对象。

When you access them within a method, eg with self.count , you get an instance-specific result that behaves like a normal non-property attribute, even though it's really a property object, because the property internally takes care of returning the right thing (I think it's right to say it's a descriptor ). 当您在方法中(例如,使用self.count访问它们时,您会得到一个特定于实例的结果,该结果的行为类似于普通的非属性属性, 即使它实际上是一个属性对象,因为该属性在内部负责返回正确的事物(我认为说它是一个描述符是正确的)。

What's happening here is that you're accessing the result at class level, when it doesn't have any special behaviour - you asked for str(count) and that's what you got, the string identifying the property object. 这里发生的是,您在类级别访问结果时,如果它没有任何特殊行为-您要求提供str(count) ,这就是您所得到的,即标识属性对象的字符串。

Probably the correct pythonic way to resolve this is 解决此问题的正确方法可能是

class Kecks(Widget):
    count = NumericProperty(0)
    amount = NumericProperty(1)
    txt = StringProperty()

    def __init__(self, *args, **kwargs):
        super(Kecks, self).__init__(*args, **kwargs):
        self.txt = str(self.count)

By setting the value in __init__ , you get the correct instance-level behaviour. 通过在__init__设置值,您可以获得正确的实例级行为。 You can also do stuff like mess around with accessing count.defaultvalue to get its actual value, but this is probably a bad idea. 您还可以通过访问count.defaultvalue来获得诸如实际值之类的东西,但这可能不是一个好主意。

If you want txt to be bound to the value of count (automatically changing to track str(count)), you have to do more again, but I'm not clear if that's your intention so I won't go into it and you can check the doc anyway. 如果您想让txt 绑定到count的值(自动更改为跟踪str(count)),则必须再次执行更多操作,但是我不清楚这是否是您的意图,因此我不再赘述仍然可以检查文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM