Как не дать пользователям пройти мимо экрана входа в приложение до входа в Kivy
Первый экран, к которому он идет, - это экран входа в систему, но я могу просто нажать любую кнопку на панели действий в обход экрана входа в систему. Я даже думал об удалении кнопок Панели Действия, тогда я мог просто вернуть их обратно, если вход в систему успешен, но я не мог понять, как вызвать идентификаторы Панели Действия из класса экрана входа в систему. Каков наилучший способ не дать пользователю обойти экран входа в приложение?
Код 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()
Основное кв:
<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 ответ
Решение
Использовать BooleanProperty
а также ActionButton
"s disabled
атрибут (неактивные кнопки ActionButtons), чтобы запретить пользователям доступ к другим экранам.
Код Python
- Добавьте BooleanProperty к оператору импорта,
from kivy.properties import ObjectProperty, BooleanProperty
- Добавить BooleanProperty,
access_denied = BooleanProperty(True)
вclass Menu()
- В
verify_credentials()
метод, если пароль совпадает, установитеaccess_denied
вFalse
иначе установите его наTrue
,
отрывок
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
файл kv
- Проверьте BooleanProperty,
access_denied
в каждой кнопке ActionButton
отрывок
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'
Диспетчер свойств экрана по умолчанию
Каждый экран имеет по умолчанию менеджер свойств, который предоставляет вам экземпляр используемого ScreenManager.
пример
main.py
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()
main.kv
#:kivy 1.11.0
<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'
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'
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>:
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'
ScreenNearUsers:
id: screen_near_users
name: 'near_users'
ScreenMatch:
id: screen_match
name: 'match'
ScreenChats:
id: screen_chats
name: 'chats'
ScreenUserProfile:
id: screen_user_profile
name: 'profile'