[英]NameError: name ' ' not defined in .py/.kv
I have been trying to create an app with Kivy that receives 0, 1 or 2 as text input by the user (it will be specified in the first screen that the user should only type 0, 1 or 2 as input) and in the final screen shows a label text which differs based on that input.我一直在尝试使用 Kivy 创建一个应用程序,该应用程序接收 0、1 或 2 作为用户输入的文本(将在第一个屏幕中指定用户只应输入 0、1 或 2 作为输入)并在最后屏幕显示 label 文本,该文本因输入而异。 When I try to run it, it will not start due to a NameError in the final line of code in the.kv file.当我尝试运行它时,由于 .kv 文件中最后一行代码中的 NameError,它不会启动。
What I would like to know is how i can define the name in the NameError or if I can define the ids of the text inputs as variables in order to overcome the error.我想知道的是如何在 NameError 中定义名称,或者是否可以将文本输入的 id 定义为变量以克服错误。
I do realise a lot of very similar questions have been asked before here and on other sites and I have tried implementing the answers, yet they do not work.我确实意识到之前在这里和其他网站上已经提出了很多非常相似的问题,我已经尝试实施答案,但它们不起作用。 My background in programming is practically non-existent, so please be as elaborate as you can in your answers if you feel like it.我的编程背景几乎不存在,所以如果您愿意,请尽可能详细地回答您的问题。 Thank you in advance for your time and following are the codes of the.py and.kv files as well as a part the error.提前感谢您的宝贵时间,以下是 .py 和 .kv 文件的代码以及部分错误。
Edit: Thank you all for your comments and answers.编辑:谢谢大家的评论和回答。 The error has been fixed by implementing your recommendations and I am now able to run the app.该错误已通过实施您的建议得到修复,我现在可以运行该应用程序。 However, another problem has come up.然而,另一个问题出现了。 On the fourth screen only the text of the "else statement" ("Bad Luck") is printed in the label even when the requirements of the "if statement" are met.在第四个屏幕上,即使满足“if 语句”的要求,label 中也只会打印“else statement”(“Bad Luck”)的文本。 Any ideas or suggestions would be deeply appreciated.任何想法或建议将不胜感激。
.py file: .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 file: .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"
Error:错误:
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
To get a screen from the screen manager either you have to use id
of the screen or use get_screen("screen_name")
.要从屏幕管理器获取屏幕,您必须使用屏幕的id
或使用get_screen("screen_name")
。 In your code, I don't see any one them.在您的代码中,我没有看到任何一个。 What you can do is give all screens an id
and then use root.manager.<screen_id>.ti_a.text
and so on.您可以做的是给所有屏幕一个id
,然后使用root.manager.<screen_id>.ti_a.text
等等。 Or you can use get_screen
to get screen by its name which you have given in your code.或者您可以使用get_screen
根据您在代码中给出的名称来获取屏幕。 It will be like root.manager.get_screen("second").ti_a.text
and so on.它会像root.manager.get_screen("second").ti_a.text
等等。 I would say use later one as you have already given names.我会说稍后使用,因为您已经给出了名称。 This is how it will look:这是它的外观:
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.