如何在登录Kivy之前防止用户越过App的登录屏幕

时间:2018-07-25 21:37:18

标签: python kivy kivy-language

它进入的第一个屏幕是登录屏幕,但是,我可以单击操作栏上的任何按钮来绕过登录屏幕。我什至想到要删除操作栏按钮,如果登录成功,我可以将它们放回去,但是我不知道如何从登录屏幕类中调用操作栏的ID。阻止用户绕过应用程序登录屏幕的最佳方法是什么?

Python代码:

from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.clock import mainthread
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView


Builder.load_file('main.kv')


class Menu(BoxLayout):
    manager = ObjectProperty(None)


class ScreenLogIn(Screen):




    @mainthread
    def verify_credentials(self):


        try:



           if self.ids.login.text == "email@email.com" and self.ids.passw.text == "password":
            self.manager.current = "match"
        else:

            popup = Popup(title='Try again',
                          content=Label(text='Wrong Email/Password'),
                          size_hint=(None, None), size=(400, 400),
                          auto_dismiss=True)
            popup.open()
    except Exception as e:
        pass


class ScreenNearUsers(Screen):

    @mainthread
    def on_enter(self):

    for i in xrange(101):
        button = Button(text="B_" + str(i))
        self.ids.grid.add_widget(button)


class ScreenMatch(Screen):
    pass


class ScreenChats(Screen):
    pass


class ScreenUserProfile(Screen):
    pass


class Manager(ScreenManager):
    screen_log_in = ObjectProperty(None)
    screen_near_user = ObjectProperty(None)
    screen_match = ObjectProperty(None)
    screen_chats = ObjectProperty(None)
    screen_user_profile = ObjectProperty(None)


class MenuApp(App):

    def build(self):
       return Menu()


if __name__ == '__main__':
       MenuApp().run()

主要kv:

<Menu>:
    manager: screen_manager
    orientation: "vertical"
    id: action
ActionBar:
    size_hint_y: 0.1
    background_color: 0, 0, 1000, 10
    background_normal: ""
    ActionView:
        ActionPrevious:
        ActionButton:
            id: near_users
            icon: 'icons/internet.png'
            on_press: root.manager.current = 'near_users'
        ActionButton:
            id: matching_bar
            text: "Matching"
            on_press: root.manager.current= 'match'
        ActionButton:
            id: chat
            text: "chat"
            on_press: root.manager.current = 'chats'
        ActionButton:
            id: profile
            text: "Profile"
            on_press: root.manager.current = 'profile'
Manager:
    id: screen_manager

<ScreenLogIn>:
     orientation: "vertical"
     id: login_screen
     BoxLayout:

     TextInput:
         id: login
     TextInput:
         id: passw
         password: True # hide password
     Button:
         text: "Log In"
         on_release: root.verify_credentials()

<ScreenNearUsers>:
    ScrollView:
        GridLayout:
            id: grid
            size_hint_y: None
            height: self.minimum_height
            cols: 2
            row_default_height: '20dp'
            row_force_default: True
            spacing: 0, 0
            padding: 0, 0

<ScreenMatch>:
    Button:
        text:

<ScreenChats>:
    Button:
        text: "stuff3"

<ScreenUserProfile>:
    Button:
        text: "stuff4"

<Manager>:
    id: screen_manager
    screen_log_in: screen_log_in
    screen_near_users: screen_near_users
    screen_match: screen_match
    screen_chats: screen_chats
    screen_user_profile: screen_user_profile

ScreenLogIn:
    id: screen_log_in
    name: 'login'
    manager: screen_manager
ScreenNearUsers:
    id: screen_near_users
    name: 'near_users'
    manager: screen_manager
ScreenMatch:
    id: screen_match
    name: 'match'
    manager: screen_manager
ScreenChats:
    id: screen_chats
    name: 'chats'
    manager: screen_manager
ScreenUserProfile:
    id: screen_user_profile
    name: 'profile'
    manger: screen_manager

1 个答案:

答案 0 :(得分:0)

解决方案

使用BooleanPropertyActionButton的{​​{1}}属性(灰色的ActionButton)来防止用户访问其他屏幕。

Python代码

  1. 将BooleanProperty添加到import语句disabled
  2. from kivy.properties import ObjectProperty, BooleanProperty中添加BooleanProperty access_denied = BooleanProperty(True)
  3. class Menu()方法中,如果密码匹配,请将verify_credentials()设置为access_denied,否则将其设置为False

摘要

True

kv文件

  1. 在每个ActionButton中检查BooleanProperty from kivy.properties import ObjectProperty, BooleanProperty ... class Menu(BoxLayout): access_denied = BooleanProperty(True) class ScreenLogIn(Screen): def verify_credentials(self): try: if self.ids.login.text == "email@email.com" and self.ids.passw.text == "password": App.get_running_app().root.access_denied = False self.manager.current = "match" else: App.get_running_app().root.access_denied = True popup = Popup(title='Try again', content=Label(text='Wrong Email/Password'), size_hint=(None, None), size=(400, 400), auto_dismiss=True) popup.open() except Exception as e: pass

摘要

access_denied

Screen's default property manager

  

每个屏幕默认都有一个属性管理器,可为您提供   使用的ScreenManager实例。

示例

main.py

        ActionButton:
            id: near_users
            icon: 'icons/internet.png'
            disabled: True if root.access_denied else False
            on_press: root.manager.current = 'near_users'

        ActionButton:
            id: matching_bar
            text: "Matching"
            disabled: True if root.access_denied else False
            on_press: root.manager.current= 'match'

        ActionButton:
            id: chat
            text: "chat"
            disabled: True if root.access_denied else False
            on_press: root.manager.current = 'chats'

        ActionButton:
            id: profile
            text: "Profile"
            disabled: True if root.access_denied else False
            on_press: root.manager.current = 'profile'

main.kv

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, BooleanProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button


Builder.load_file('main.kv')


class Menu(BoxLayout):
    access_denied = BooleanProperty(True)


class ScreenLogIn(Screen):

    def verify_credentials(self):
        try:
            if self.ids.login.text == "email@email.com" and self.ids.passw.text == "password":
                App.get_running_app().root.access_denied = False
                self.manager.current = "match"
            else:
                App.get_running_app().root.access_denied = True
                popup = Popup(title='Try again',
                              content=Label(text='Wrong Email/Password'),
                              size_hint=(None, None), size=(400, 400),
                              auto_dismiss=True)
                popup.open()
        except Exception as e:
            pass


class ScreenNearUsers(Screen):

    def on_enter(self):
        for i in range(101):
            button = Button(text="B_" + str(i))
            self.ids.grid.add_widget(button)


class ScreenMatch(Screen):
    pass


class ScreenChats(Screen):
    pass


class ScreenUserProfile(Screen):
    pass


class Manager(ScreenManager):
    screen_log_in = ObjectProperty(None)
    screen_near_user = ObjectProperty(None)
    screen_match = ObjectProperty(None)
    screen_chats = ObjectProperty(None)
    screen_user_profile = ObjectProperty(None)


class MenuApp(App):

    def build(self):
        return Menu()


if __name__ == '__main__':
    MenuApp().run()

输出

Img01 - ActionButton grayed out Img02 - clicked ActionButton near_users