如何在kivy中停止转换到下一个屏幕

问题描述 投票:1回答:1

我按下了一个登录页面和一个按钮,它进入下一个屏幕,我使用on_press我想在用户名和密码为false时打印一些内容并且不进入下一个屏幕:

<loginView>:



    status:result
    Label:
            text:"Login"
            pos : 0,250
            font_size:40

    Label:
            text:"Username"
            pos:-75,125

    Label:
            text:"Password"
            pos:-75,50

    TextInput:
            multiline:False
            pos:400,400
            size_hint:.2,.08
            font_size:20
            id:username

    TextInput:
            multiline:False
            pos:400,325
            password:True
            size_hint:.2,.08
            font_size:20
            id:password
    Button:

            text:"Login"
            size_hint:.1,.07
            pos:290,270
            color:1,0,0,1

            on_press:root.manager.current ='settings'
    Label:
            text:""
            pos:600,100
            id:result



<afterLogin>:
        ScrollView:
                GridLayout:
                        pos: 0, 0
                        size: 100, 100
                        cols: 1
                        rows: 70
                        on_parent:
                                orientation:'vertical'
                                [self.add_widget(Button(text=str(i))) for i in range(1, 70)]

                        size_hint:.5, 4
                        size:300,300
python kivy kivy-language
1个回答
0
投票

解决方案有两种方法,即在Python代码或kv文件中检查用户的名称和密码。

Method 1

签入kv文件。

片段

on_press:
    if username.text == 'user' and password.text == 'kivy': \
    result.text = ''; \
    root.manager.current = 'settings' 
    else: result.text = 'Invalid username and/or password!'

Method 2

更改为kv文件并实现一个名为authentication()的新方法

Python code

class LoginView(Screen):
    status = ObjectProperty(None)

    def authentication(self):
        if self.ids.username.text == "admin" and self.ids.password.text == "kivy":
            self.status.text = ''
            self.manager.current = 'settings'
        else:
            self.status.text = "Invalid username and/or password!"

方圆角

on_press:
    root.authentication()
© www.soinside.com 2019 - 2024. All rights reserved.