[英]NameError: name ' ' not defined in .py/.kv
我一直在尝试使用 Kivy 创建一个应用程序,该应用程序接收 0、1 或 2 作为用户输入的文本(将在第一个屏幕中指定用户只应输入 0、1 或 2 作为输入)并在最后屏幕显示 label 文本,该文本因输入而异。 当我尝试运行它时,由于 .kv 文件中最后一行代码中的 NameError,它不会启动。
我想知道的是如何在 NameError 中定义名称,或者是否可以将文本输入的 id 定义为变量以克服错误。
我确实意识到之前在这里和其他网站上已经提出了很多非常相似的问题,我已经尝试实施答案,但它们不起作用。 我的编程背景几乎不存在,所以如果您愿意,请尽可能详细地回答您的问题。 提前感谢您的宝贵时间,以下是 .py 和 .kv 文件的代码以及部分错误。
编辑:谢谢大家的评论和回答。 该错误已通过实施您的建议得到修复,我现在可以运行该应用程序。 然而,另一个问题出现了。 在第四个屏幕上,即使满足“if 语句”的要求,label 中也只会打印“else statement”(“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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.