[英]Kivy: How can I connect TextInput in .KV file to a variable in Python?
I'm trying to create a simple login page for this app using Kivy.我正在尝试使用 Kivy 为这个应用程序创建一个简单的登录页面。
I'm new to this, and I'm wondering how I can connect the Email TextInput to a variable ( email_catch
) in my python code, similar to a normal.get() function.我是新手,我想知道如何将 Email TextInput 连接到我的 python 代码中的变量(
email_catch
),类似于 normal.get() ZC1C425268E68385D1AB5074C17A94F。
Python Code Python代码
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, StringProperty
class Login_Window(Screen):
def verify(self):
email_catch = self.root.ids.email.text
print(email_catch)
class MyApp(App):
def build(self):
return Login_Window()
if __name__ == "__main__":
MyApp().run()
.KV File .KV 文件
#:kivy 2.0.0
<Login_Window>:
GridLayout:
cols:1
size: root.width, root.height
GridLayout:
cols:2
Label:
text: 'Email'
TextInput:
multiline: False
id: email
Button:
text: 'Log In'
on_press: root.verify()
Since you are calling root
in verify
method of Login_Window
class (where it ( root
) hasn't been defined yet) and not in the build
method of the App
class you are supposed to get an AttributeError
.由于您在
Login_Window
class 的verify
方法中调用root
(尚未定义它( root
)而不是在App
class 的build
方法中调用,因此您应该得到一个AttributeError
。
In order to access an id
within that class you should use self.ids
.为了访问该 class 中的
id
,您应该使用self.ids
。 So the change you need:所以你需要的改变:
def verify(self):
email_catch = self.ids.email.text
print(email_catch)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.