[英]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.