NameError: name ' ' not defined in .py/.kv

NameError: name ' ' not defined in .py/.kv

我一直在尝试使用 Kivy 创建一个应用程序,它接收 0、1 或 2 作为用户输入的文本(它将在第一个屏幕中指定用户只能输入 0、1 或 2 作为输入) 并在最终屏幕中显示根据该输入而不同的标签文本。当我尝试 运行 它时,由于 .kv 文件最后一行代码中的 NameError,它不会启动。

我想知道如何在 NameError 中定义名称,或者我是否可以将文本输入的 ID 定义为变量以克服错误。

我确实意识到之前在这里和其他网站上已经问过很多非常相似的问题,我已经尝试实施答案,但它们不起作用。我的编程背景几乎不存在,所以如果您愿意,请尽可能详细地回答您的问题。预先感谢您的宝贵时间,以下是 .py 和 .kv 文件的代码以及部分错误。

编辑:感谢大家的评论和回答。该错误已通过实施您的建议得到修复,我现在可以 运行 该应用程序。然而,另一个问题出现了。在第四个屏幕上,即使满足“if 语句”的要求,标签中也只会打印“else 语句”(“Bad Luck”)的文本。任何想法或建议将不胜感激。

.py 文件:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty, StringProperty, NumericProperty


class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class ThirdWindow(Screen):
    pass

class FourthWindow(Screen):
    pass
    
    
class WindowManager(ScreenManager):
    pass


class MyApp(App):
    def build(self):
       
        kv = Builder.load_file("my.kv")
        return kv
        
     
    
if __name__=="__main__":
    MyApp().run()

.kv 文件:

WindowManager:
    MainWindow:
    SecondWindow:
    ThirdWindow:
    FourthWindow:
    

<MainWindow>:
    name: "main"
    GridLayout:
        cols:1
        rows:2
        Label:
            text: "stuff"
        Button:
            text: "stuff"
            on_release:
                app.root.current = "second"
                root.manager.transition.direction = "left"

<SecondWindow>:
    name: "second"
    GridLayout:
        cols:1
        rows:2

        GridLayout:
            cols:2
            Label:
                text: "stuff"
            TextInput:
                id: ti_a
                multiline:False
                
            Label:
                text: "stuff"
            TextInput:
                id: ti_b
                multiline:False

            Label:
                text: "stuff"
            TextInput:
                id: ti_c
                multiline:False

            Label:
                text: "stuff"
            TextInput:
                id: ti_d
                multiline:False

            Label:
                text: "stuff"
            TextInput:
                id: ti_e
                multiline:False

        GridLayout:
            cols:2 
        
            Button:
                text: "stuff"
                on_release:
                    app.root.current = "third"
                    root.manager.transition.direction = "left"
            Button:
                text: "Back"
                on_release:
                    app.root.current = "main"
                    root.manager.transition.direction = "right"

<ThirdWindow>:
    name: "third"
    GridLayout:
        cols:1
        rows:2

        GridLayout:
            cols:2
            Label:
                text: "stuff"
            TextInput:
                id: ti_f
                multiline:False

        GridLayout:
            cols:2

            Button:
                text: "stuff"
                on_release:
                    app.root.current = "fourth"
                    root.manager.transition.direction = "left"

            Button:
                text: "Back"
                on_release:
                    app.root.current = "second"
                    root.manager.transition.direction = "right"


<FourthWindow>:
    name: "fourth"
    Label:
        text:"stuff" if root.manager.screens(second).ids.ti_a.text == "0" and root.manager.screens(second).ids.ti_b.text == "0" and root.manager.screens(second).ids.ti_c.text == "0" and root.manager.screens(second).ids.ti_d.text == "0" and root.manager.screens(second).ids.ti_e.text == "1" and root.manager.screens(third).ids.ti_f.text == "0" else "Bad Luck"

错误:

BuilderException: Parser: File "C:\Users\NIK\Desktop\my.kv", line 106:
     104:    name: "fourth"
     105:    Label:
 >>  106:        text:"stuff" if root.manager.screens(second).ids.ti_a.text == "0" and root.manager.screens(second).ids.ti_b.text == "0" and root.manager.screens(second).ids.ti_c.text == "0" and root.manager.screens(second).ids.ti_d.text == "0" and root.manager.screens(second).ids.ti_e.text == "1" and root.manager.screens(third).ids.ti_f.text == "0" else "Bad Luck"  
     107:                    
     108:
 NameError: name 'second' is not defined

要从屏幕管理器获取屏幕,您必须使用屏幕的 id 或使用 get_screen("screen_name")。在您的代码中,我没有看到任何一个。你可以做的是给所有屏幕一个 id 然后使用 root.manager.<screen_id>.ti_a.text 等等。或者您可以使用 get_screen 通过您在代码中提供的名称获取屏幕。它会像 root.manager.get_screen("second").ti_a.text 等等。我会说以后使用一个,因为你已经给出了名字。这是它的样子:

text:"stuff" if root.manager.get_screen("second").ids.ti_a.text == "0" and root.manager.get_screen("second").ids.ti_b.text == "0" and root.manager.get_screen("second").ids.ti_c.text == "0" and root.manager.get_screen("second").ids.ti_d.text == "0" and root.manager.get_screen("second").ids.ti_e.text == "1" and root.manager.get_screen("third").ids.ti_f.text == "0" else "Bad Luck"