Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.app import App
- from kivy.properties import StringProperty, ObjectProperty
- from kivy.lang import Builder
- from kivy.event import EventDispatcher
- KV = '''
- #:import A kivy.animation.Animation
- BoxLayout:
- orientation: 'vertical'
- Label:
- size_hint_y: None
- height: 50
- text: 'An Awesome App'
- ScreenManager:
- id: screenmanager
- LoginScreen:
- WelcomeScreen:
- <AutoLabel@Label>:
- size_hint: None, None
- size: self.texture_size
- valign: 'center'
- <FormTextInput@TextInput>:
- size_hint: None, None
- multiline: False
- height: self.minimum_height
- width: 200
- <FormButton@Button>:
- size_hint_y: None
- height: 50
- <LoginScreen@Screen>:
- name: 'login'
- BoxLayout:
- id: box
- orientation: 'vertical'
- pos_hint: {'center': (.5, .5)}
- size_hint: None, None
- size: self.minimum_size
- xoff: 0
- # this is just to shake the box if login fails
- canvas.before:
- PushMatrix:
- Translate:
- x: self.xoff or 0
- canvas.after:
- PopMatrix:
- GridLayout:
- size_hint: None, None
- size: self.minimum_size
- cols: 2
- AutoLabel:
- text: 'username'
- FormTextInput:
- id: username
- AutoLabel:
- text: 'password'
- FormTextInput:
- id: password
- password: True
- FormButton:
- text: 'login'
- on_release:
- success = app.login_manager.login(username.text, password.text)
- if success: app.goto_screen('welcome')
- else: A.cancel_all(box), (
- A(xoff=-50, t='out_quad', d=.1)
- + A(xoff=0, t='out_elastic', d=.4)
- ).start(box)
- <WelcomeScreen@Screen>:
- name: 'welcome'
- BoxLayout:
- orientation: 'vertical'
- Label:
- text: 'welcome {username}'.format(username=app.login_manager.username)
- FormButton:
- text: 'logout'
- on_release:
- app.login_manager.logout()
- app.goto_screen('login')
- '''
- class LoginManager(EventDispatcher):
- username = StringProperty()
- def login(self, username, password):
- # here one would do a proper check
- if username == 'admin' and password == 'admin':
- self.username = username
- return True
- else:
- return False
- def logout(self):
- self.username = ''
- class MyApp(App):
- login_manager = ObjectProperty()
- def build(self):
- self.login_manager = LoginManager()
- return Builder.load_string(KV)
- def goto_screen(self, name):
- self.root.ids.screenmanager.current = name
- if __name__ == '__main__':
- app = MyApp()
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement