简体   繁体   English

打印NumericProperty()kivy的值

[英]print the value of a NumericProperty() kivy

If I have a Numeric Property in two of my screens that count how many times a person clicked the correct icon and I want to print it in a separate class how would I do this? 如果我在两个屏幕上都有一个数字属性,该属性计算一个人单击了正确图标的次数,并且我想在一个单独的班级中打印它,我该怎么做? I have tried using the print() function and using print(StringProperty(str())) but I dont get a number value printed. 我尝试使用print()函数和print(StringProperty(str()))但是我没有得到打印的数值。 When I print the correct_counter in VerifyAA() and VerifyBB() the correct value is printed. 当我在VerifyAA()VerifyBB()打印correct_counter时,将打印正确的值。

class A(Screen):
    correct_counter1 = NumericProperty(0)
    def verifyAA(self, *args):
        if self.AA == V.Check1 or self.AA == V.Check2 or self.AA == V.Check3:
            print("You got it!!!")
            self.correct_counter1 =  self.correct_counter1 + 1
            print(self.correct_counter1)
            self.ids.aa.disabled = True

class B(Screen):
    correct_counter2 = NumericProperty(0)
    def verifyBB(self, *args):
        if self.BB == VV.Check1 or self.BB == VV.Check2 or self.BB == VV.Check3:
            print("You got it!!!")
            self.correct_counter2 =  self.correct_counter2 + 1
            print(self.correct_counter2)
            self.ids.bb.disabled = True

class Answers(Screen):
    print(A.correct_counter1)
    print(StringProperty(str(B.correct_counter2)))

This is what gets printed respectively: 这是分别打印的内容:

<NumericProperty name=correct_counter>
<StringProperty name=>

You need to understand the distinction between the definition of a class and an instance of a class. 您需要了解类的定义和类的实例之间的区别。 When you print A.correct_counter1 you print the NumericProperty object itself, defined at class level. 当您打印A.correct_counter1您将打印在类级别定义的NumericProperty对象本身。 That's the only sensible result - it wouldn't make sense for it to print the value of the property, because it doesn't have a value. 这是唯一合理的结果-这是没有意义的它打印属性的值,因为它不具有价值。 For instance, if you wrote instance1 = A(correct_counter1=1); instance2 = A(correct_counter1=20) 例如,如果您编写了instance1 = A(correct_counter1=1); instance2 = A(correct_counter1=20) instance1 = A(correct_counter1=1); instance2 = A(correct_counter1=20) what value would you have expected print(A.correct_counter1) to print? instance1 = A(correct_counter1=1); instance2 = A(correct_counter1=20)您希望将print(A.correct_counter1)打印什么值? The answer has to be neither, the value of the property is defined in each case for the class instance, not the class itself. 答案不必是两者,属性值是在每种情况下为类实例定义的,而不是为类本身定义的。

The correct solution is that you must print the value of the property via an instance of your class. 正确的解决方案是您必须通过类的实例打印属性的值。 For instance, if you write instance = A(); print(instance.correct_counter1) 例如,如果您编写instance = A(); print(instance.correct_counter1) instance = A(); print(instance.correct_counter1) you will print a number like you expect. instance = A(); print(instance.correct_counter1)您将打印一个期望的数字。 The best way to do this depends on the structure of your program and the relationship between the classes. 最好的方法取决于程序的结构以及类之间的关系。 Your example isn't a runnable program so it isn't possible to make a specific suggestion. 您的示例不是可运行的程序,因此无法提出具体建议。 If you have an actual example where you want to do this but can't work out how, post that as a new question. 如果您有一个实际的示例想要在其中执行此操作,但无法解决问题,请将其作为新问题发布。

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

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