【发布时间】:2021-01-12 17:02:18
【问题描述】:
我一直在尝试使用 Kivy 创建一个应用程序,该应用程序接收 0、1 或 2 作为用户输入的文本(将在第一个屏幕中指定用户只应输入 0、1 或 2 作为输入)和在最终屏幕中显示一个标签文本,该文本根据该输入而有所不同。当我尝试运行它时,由于 .kv 文件中最后一行代码中的 NameError,它无法启动。
我想知道的是如何在 NameError 中定义名称,或者是否可以将文本输入的 id 定义为变量以克服错误。
我确实意识到之前在这里和其他网站上已经提出了很多非常相似的问题,我已经尝试实施答案,但它们不起作用。我的编程背景几乎不存在,所以如果您愿意,请尽可能详细地回答您的问题。提前感谢您的宝贵时间,以下是 .py 和 .kv 文件的代码以及部分错误。
编辑:感谢大家的 cmets 和回答。该错误已通过实施您的建议得到修复,我现在可以运行该应用程序。然而,另一个问题出现了。在第四个屏幕上,即使满足“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
【问题讨论】:
-
这里的错误信息似乎很清楚。我在您的代码中没有看到您定义
second的任何地方。不是那么简单吗?我在这里错过了什么吗? -
你可能需要
text:"stuff" if root.manager.get_screen("second").ids.ti_...。 -
如果您有单独的问题,请提出单独的问题。