【问题标题】:how can i stop transition to next screen in kivy我如何在kivy中停止过渡到下一个屏幕
【发布时间】:2018-09-09 10:50:49
【问题描述】:

我有一个登录页面和一个按钮,按下它会转到下一个屏幕,我使用on_press 我想在用户名和密码为假的时候打印一些东西,并且不要进入下一个屏幕:

<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】:

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

    方法一

    正在检查 kv 文件。

    sn-p

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

    方法二

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

    Python 代码

    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!"
    

    kv 文件

    on_press:
        root.authentication()
    

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多