Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.properties import StringProperty, ObjectProperty
  3. from kivy.lang import Builder
  4. from kivy.event import EventDispatcher
  5.  
  6. KV = '''
  7. #:import A kivy.animation.Animation
  8.  
  9. BoxLayout:
  10. orientation: 'vertical'
  11. Label:
  12. size_hint_y: None
  13. height: 50
  14. text: 'An Awesome App'
  15.  
  16. ScreenManager:
  17. id: screenmanager
  18. LoginScreen:
  19. WelcomeScreen:
  20.  
  21. <AutoLabel@Label>:
  22. size_hint: None, None
  23. size: self.texture_size
  24. valign: 'center'
  25.  
  26. <FormTextInput@TextInput>:
  27. size_hint: None, None
  28. multiline: False
  29. height: self.minimum_height
  30. width: 200
  31.  
  32. <FormButton@Button>:
  33. size_hint_y: None
  34. height: 50
  35.  
  36. <LoginScreen@Screen>:
  37. name: 'login'
  38.  
  39. BoxLayout:
  40. id: box
  41. orientation: 'vertical'
  42. pos_hint: {'center': (.5, .5)}
  43. size_hint: None, None
  44. size: self.minimum_size
  45. xoff: 0
  46.  
  47. # this is just to shake the box if login fails
  48. canvas.before:
  49. PushMatrix:
  50. Translate:
  51. x: self.xoff or 0
  52.  
  53. canvas.after:
  54. PopMatrix:
  55.  
  56. GridLayout:
  57. size_hint: None, None
  58. size: self.minimum_size
  59. cols: 2
  60.  
  61. AutoLabel:
  62. text: 'username'
  63. FormTextInput:
  64. id: username
  65. AutoLabel:
  66. text: 'password'
  67. FormTextInput:
  68. id: password
  69. password: True
  70.  
  71. FormButton:
  72. text: 'login'
  73. on_release:
  74. success = app.login_manager.login(username.text, password.text)
  75. if success: app.goto_screen('welcome')
  76. else: A.cancel_all(box), (
  77. A(xoff=-50, t='out_quad', d=.1)
  78. + A(xoff=0, t='out_elastic', d=.4)
  79. ).start(box)
  80.  
  81.  
  82. <WelcomeScreen@Screen>:
  83. name: 'welcome'
  84.  
  85. BoxLayout:
  86. orientation: 'vertical'
  87.  
  88. Label:
  89. text: 'welcome {username}'.format(username=app.login_manager.username)
  90.  
  91. FormButton:
  92. text: 'logout'
  93. on_release:
  94. app.login_manager.logout()
  95. app.goto_screen('login')
  96. '''
  97.  
  98. class LoginManager(EventDispatcher):
  99. username = StringProperty()
  100.  
  101. def login(self, username, password):
  102. # here one would do a proper check
  103. if username == 'admin' and password == 'admin':
  104. self.username = username
  105. return True
  106. else:
  107. return False
  108.  
  109. def logout(self):
  110. self.username = ''
  111.  
  112.  
  113. class MyApp(App):
  114. login_manager = ObjectProperty()
  115.  
  116. def build(self):
  117. self.login_manager = LoginManager()
  118. return Builder.load_string(KV)
  119.  
  120. def goto_screen(self, name):
  121. self.root.ids.screenmanager.current = name
  122.  
  123.  
  124. if __name__ == '__main__':
  125. app = MyApp()
  126. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement