Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将文本输入数据作为变量收集以在If语句中使用_Python_Kivy - Fatal编程技术网

Python 如何将文本输入数据作为变量收集以在If语句中使用

Python 如何将文本输入数据作为变量收集以在If语句中使用,python,kivy,Python,Kivy,我需要知道在kivy的文本框中收集用户输入数据的语法,其目的是使屏幕左下角的login按钮发挥应有的功能。我想让程序能够收集用户在密码字段中输入的内容,然后使用if语句确定密码是否正确 我知道这是可能的,我只是不知道使用什么语法,文档中也没有太多关于文本框的内容 Python文件: import kivy from kivy.app import App kivy.require("1.10.1") from kivy.uix.label import Label from kivy.uix.b

我需要知道在kivy的文本框中收集用户输入数据的语法,其目的是使屏幕左下角的login按钮发挥应有的功能。我想让程序能够收集用户在密码字段中输入的内容,然后使用if语句确定密码是否正确

我知道这是可能的,我只是不知道使用什么语法,文档中也没有太多关于文本框的内容

Python文件:

import kivy
from kivy.app import App
kivy.require("1.10.1")
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.textinput import TextInput

class Screen1(Screen):
    pass

class Screen2(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

render = Builder.load_file("kvinterp.kv")

class MainApp(App):

    def build(self):
        return render

if __name__ == "__main__":
    MainApp().run()
.kv文件:

ScreenManager:
    Screen1:
    Screen2:

<Screen1>:
    name: "Screen1"
    Label:
        text: "Please Enter The Correct Password"
        pos_hint: {"x": .45, 'y':.9}
        size_hint: .1, .1
        font_size: 40
    TextInput:
        hint_text: "Password"
        size_hint: 0.3, 0.1
        pos_hint: {"x": 0.35, 'y': 0.5}
        multiline: False
    Button:
        text: "Login"
        on_release: app.root.current = "Screen2"
        size_hint: 0.17, 0.16
        pos_hint: {"x": 0, 'y':0}
        background_color: 1.23, 1.56, 1.70, .5

<Screen2>:
    name: "Screen2"
    Label:
        text: "You've Logged In!"
    Button:
        text: "Return"
        on_release: app.root.current = "Screen1"
        size_hint: 0.17, 0.16
        pos_hint: {"x": 0, 'y':0}
        background_color: 1.23, 1.56, 1.70, .5
屏幕管理器:
屏幕1:
屏幕2:
:
名称:“屏幕1”
标签:
文本:“请输入正确的密码”
pos_提示:{“x”:.45,'y':.9}
大小提示:.1,.1
字体大小:40
文本输入:
提示文字:“密码”
大小提示:0.3,0.1
pos_提示:{“x”:0.35,“y”:0.5}
多行:False
按钮:
文本:“登录”
发布时:app.root.current=“Screen2”
尺寸提示:0.17,0.16
pos_提示:{“x”:0,“y”:0}
背景颜色:1.23,1.56,1.70,5
:
名称:“屏幕2”
标签:
文本:“您已登录!”
按钮:
文本:“返回”
发布时:app.root.current=“Screen1”
尺寸提示:0.17,0.16
pos_提示:{“x”:0,“y”:0}
背景颜色:1.23,1.56,1.70,5

按下按钮时将文本传递给函数,但要识别元素,必须建立
id

*.py

# ...

class Screen1(Screen):
    def validate_password(self, password):
        if password == "123456":
            self.manager.current = "Screen2"

# ...
*.kv

# ...

<Screen1>:
    name: "Screen1"
    Label:
        text: "Please Enter The Correct Password"
        pos_hint: {"x": .45, 'y':.9}
        size_hint: .1, .1
        font_size: 40
    TextInput:
        id: password_ti # <---
        hint_text: "Password"
        size_hint: 0.3, 0.1
        pos_hint: {"x": 0.35, 'y': 0.5}
        multiline: False
    Button:
        text: "Login"
        on_press: root.validate_password(password_ti.text) # <---
        size_hint: 0.17, 0.16
        pos_hint: {"x": 0, 'y':0}
        background_color: 1.23, 1.56, 1.70, .5

# ...
#。。。
:
名称:“屏幕1”
标签:
文本:“请输入正确的密码”
pos_提示:{“x”:.45,'y':.9}
大小提示:.1,.1
字体大小:40
文本输入:
id:password\u ti#