【问题标题】:I need to add more than one app.root.current in kivy我需要在kivy中添加多个app.root.current
【发布时间】:2023-01-14 02:54:05
【问题描述】:

我正在 kivy 中创建一个应用程序,基本上它会在您输入日期和密码后给您一首诗。为此,我为每首诗创建了几个窗口。我的问题是我这样做是为了让按钮的 on_release 有一个类似选择性的结构来显示正确的诗。问题是这只对最后一句话有效,而不是像预期的那样同时对所有句子有效。

        Button:
            font_name: "Georgiai"
            font_size: 20
            text: "Enter"
            size_hint: 0.5, 0.5
            background_color: "#00FFCE"
            on_release:
                app.root.current = "fifth" if date.text == "01.02.2023" and password.text == "Si" else "fourth"
                app.root.current = "seis" if date.text == "02.02.2023" and password.text == "te" else "fourth"

我试过使用多个 on_release: app.root.current 但它也不起作用。这是它唯一能发挥作用的东西,所以我希望有人能帮助我。我对编程很陌生

【问题讨论】:

    标签: kivy kivy-language kivymd


    【解决方案1】:

    您应该将程序逻辑移动到 Python 中,而不是将逻辑包含在您的 kivy 文件中。参数可以这样传递:

    Button:
        on_release: app.give_poem(_date.text, _password.text, self)
        # or
        # on_release: root.give_poem(_date.text, _password.text, self)
    

    我通过添加下划线来更改您的变量,以避免将您的变量与其他常见对象混淆。我假设日期和密码是其他 kivy 对象的 ID。

    在您的 Python 应用程序或顶级小部件中,您可以使用您的逻辑。这是您的方法在您的应用程序或顶级小部件对象中应具有的基本签名。这个只是打印发送的参数,你可以从那里决定做什么。

    def give_poem(self, _date, _password, my_button):
        print(f"{self}: {_date}: {_password}: {my_button}")
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2011-10-24
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      相关资源
      最近更新 更多