【问题标题】:How to get a python dict from kivy dictProperty?如何从 kivy dictProperty 获取 python dict?
【发布时间】:2019-01-10 04:26:27
【问题描述】:

我在一个文件中创建了一些经典的 Python3 函数,使用字典作为参数。我想将此文件用作库,并使用 kivy 小部件中的功能。 所以这就是我的代码的作用:

#all kivy imports
import myLib

class MyGame(Widget):
    myDict = DictProperty({1:0, 2:0, 3:0})

    def addStuff(self, id):
        self.myDict[id]+= 1

    def myNotWorkingFunction(self):
        number = myLib.getNumberFromDict(self.myDict)
        self.text = "The result is "+str(number)

它不是那样工作的。如果我不使用myLib.getNumberFromDict,而设置number = 3,就可以了。 myLib.getNumberFromDict 与 python dict 配合得很好,因为我在尝试制作 kivy 应用程序之前使用了它。

我在 kivy 日志中有这个:

stderr: Exception KeyError: ('', ) in 'kivy.properties.observable_dict_dispatch' ignored

【问题讨论】:

    标签: python python-3.x dictionary properties kivy


    【解决方案1】:

    我成功了! 这是显而易见的方式:演员阵容!

    number = myLib.getNumberFromDict(dict(self.myDict))
    

    效果很好,我得到了预期的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2022-11-17
      相关资源
      最近更新 更多