【问题标题】:How do I access a dict value through key on Kivy Language?如何通过 Kivy 语言上的键访问 dict 值?
【发布时间】:2020-06-20 14:37:43
【问题描述】:

在我的 .py 上:

class DataGet(BoxLayout):
    def __init__(self, **kwargs):
        super(DataGet, self).__init__(**kwargs)
        self.data_get = {
            "FO2": 62,
            "Peek Pressure": 12,
            "Total Volume": 35,
            "PEEPe": 26,
            "Minute Volume": 98,
            "Respiratory Rate": 75,
        }
        self.updater = Thread(target=self.get_data, args=())

    get_fo2 = ObjectProperty(None)
    get_peepe = ObjectProperty(None)
    get_peek_pressure = ObjectProperty(None)
    get_minute_volume = ObjectProperty(None)
    get_total_volume = ObjectProperty(None)
    get_respiratory_rate = ObjectProperty(None)

    def get_data(self): #This is just a test function, not yet implemented
        while True:
            for data in self.data_get:
                self.data_get[data] = edg.update_data(self.data_get[data])
                print(self.data_get[data])
            print("###")
            print(f"fo2 = {self.data_get}")
            print("###")
            sleep(2)

在我的 .kv 上:

<DataGet>:
    get_fo2: get_fo2
    get_peepe: get_peepe
    get_peek_pressure: get_peek_pressure
    get_minute_volume: get_minute_volume
    get_total_volume: get_total_volume
    get_respiratory_rate: get_respiratory_rate
    on_kv_post: root.updater.start()
    BoxLayout:
        cols:1
        orientation: "vertical"

        BoxLayout:
            cols:2
            orientation: "vertical"
            halign: "center"

            Label:
                text: f"FO2"

            Label:
                id: get_fo2
                font_size: 60
                text: f"{root.data_get["FO2"]}"

当我调用执行代码时,终端输出:

     f"{root.data_get["FO2"]}"
                       ^
 SyntaxError: invalid syntax

这没有多大意义,除非它有不同的方式在 kv 语言上调用 dict 值。我已经阅读了 kivy 的 kv 语言文档,但没有找到任何关于如何声明和使用字典的参考资料。即使尝试了不同的语法,也没有成功。

【问题讨论】:

  • f"{root.data_get['FO2']}" kivy 部分没有可记录的内容,它是SyntaxError
  • 你的问题是一个简单的愚蠢错误,已经回答了,但它帮助我了解如何在 kivy lang 中使用 dictProperty。谢谢你的例子...

标签: python kivy kivy-language


【解决方案1】:

我认为你必须注意你的报价:

     f"{root.data_get['FO2']}"

【讨论】:

  • 是的,现在正在考虑。只是引用......?
  • @JohnAnderson 这在 cmets 中已经提到过,所以不需要重复同样的事情。
  • 需要答案,以便搜索stack overflow 的人可以看到这个问题有一个可接受的答案..
  • @JohnAnderson 首先,如果您不使用@ 提及我,我无法知道您发表了评论。其次,这类问题实际上有一个标志:“不可重现或由拼写错误引起。”由于这是一个拼写错误,因此有人有相同的拼写错误并搜索完全相同的字词以找到此问题的可能性几乎为 0。当您标记它时,它将被关闭,因此没有人会访问回答问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 2013-02-01
  • 2017-10-24
  • 2014-06-21
相关资源
最近更新 更多