|
| 1 | +from kivy.app import App |
| 2 | +from kivy.uix.label import Label |
| 3 | +from kivy.uix.textinput import TextInput |
| 4 | +from kivy.uix.floatlayout import FloatLayout |
| 5 | +from kivy.uix.gridlayout import GridLayout |
| 6 | +from kivy.uix.button import Button |
| 7 | +from kivy.uix.boxlayout import BoxLayout |
| 8 | +from kivy.metrics import dp |
| 9 | +from plyer import tts |
| 10 | + |
| 11 | +class MainApp(App): |
| 12 | + |
| 13 | + def build(self): |
| 14 | + # create root widget |
| 15 | + root_widget = FloatLayout() |
| 16 | + box = BoxLayout(orientation='vertical', |
| 17 | + size_hint=(None, None), |
| 18 | + size=(400, 200), |
| 19 | + spacing=dp(20), |
| 20 | + padding=dp(20), |
| 21 | + pos_hint={'center_x': .5, 'center_y':.5}) |
| 22 | + |
| 23 | + # create the labels and the textinputs |
| 24 | + lbl_user = Label(text='User Name:') |
| 25 | + lbl_password = Label(text='Password') |
| 26 | + ti_user = TextInput() |
| 27 | + ti_password = TextInput(password=True) |
| 28 | + |
| 29 | + # create the containers for the labels and textinputs |
| 30 | + grid_user_pass = GridLayout(rows=2, cols=2, spacing=dp(20)) |
| 31 | + # create the ok button |
| 32 | + my_but_is_ok = Button(text='OK') |
| 33 | + my_but_is_ok.bind(on_release=lambda *args: self.check_user(ti_user.text, ti_password.text)) |
| 34 | + |
| 35 | + # add the labels and input fields to the container |
| 36 | + grid_user_pass.add_widget(lbl_user) |
| 37 | + grid_user_pass.add_widget(ti_user) |
| 38 | + grid_user_pass.add_widget(lbl_password) |
| 39 | + grid_user_pass.add_widget(ti_password) |
| 40 | + |
| 41 | + # add the grid_container into it's container |
| 42 | + box.add_widget(grid_user_pass) |
| 43 | + root_widget.add_widget(box) |
| 44 | + # add the ok button at the bottom of the grid |
| 45 | + box.add_widget(my_but_is_ok) |
| 46 | + |
| 47 | + return root_widget |
| 48 | + |
| 49 | + def check_user(self, user, pwd): |
| 50 | + if pwd.lower() == 'pyconindia': |
| 51 | + tts.speak('Hello {}. Welcome to Pycon India!'.format(user)) |
| 52 | + if pwd.lower() == 'funny': |
| 53 | + tts.speak('Lalu ji ko rabbri, bahoot pasand hai!') |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + MainApp().run() |
0 commit comments