Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a login form which I have a username and password field to login. The below of password field will have a chechbox to check whether the user to show or to hide their password before they click the login button. if they click to show password, the password field will show their actual password instead of '*' and the label will change to hide password. if they uncheck the checkbox, the password will indicate otherwise, this is how it works.

Python
  1  from kivymd.app import MDApp
  2  from kivy.lang import Builder
  3  from kivy.core.window import Window
  4  from kivy.uix.screenmanager import ScreenManager
  5  from kivymd.uix.screen import MDScreen
  6  
  7  class LoginWindow(MDScreen):
  8      pass
  9  
 10  class MainWindow(MDScreen):
 11      pass
 12  
 13  class password(MDApp):
 14      def build(self):
 15          sm = ScreenManager()
 16          sm.add_widget(LoginWindow(name='login')) 
 17          sm.add_widget(MainWindow(name='main'))
 18          return sm
 19      
 20      def show_password(self, checkbox, value):
 21          if value:
 22              self.root.ids.password_login.password = False
 23              self.root.ids.password_text.text = 'Hide password'
 24          else:
 25              self.root.ids.password_login.password = True
 26              self.root.ids.password_text.text = 'Show password'
 27  
 28  Builder.load_string('''
 29  <LoginWindow>:
 30      MDFloatLayout:
 31          size_hint: .79, .08
 32          pos_hint: {"center_x": .5, "center_y": .32}
 33          
 34          MDLabel:
 35              text: "Password"
 36              font_size: "14sp"
 37              pos_hint: {"center_x": .5, "center_y": 1.2}
 38          
 39          TextInput:
 40              id: password_login
 41              password: True
 42              text: ''
 43              size_hint_y: .75
 44              pos_hint: {"center_x": .49, "center_y": .6}
 45              background_color: 0,0,0,0
 46              cursor_color: 0,0,0,1
 47              cursor_width: "2sp"
 48              font_size: "17sp"
 49              multiline: False
 50              
 51          MDFloatLayout:
 52              pos_hint:  {"center_x": .5, "center_y": .3} 
 53              size_hint_y:  .03
 54              md_bg_color: 120/255, 120/255, 120/255, 1
 55  
 56          MDCheckbox:
 57              size_hint: None, None          
 58              size: '48dp', '48dp'
 59              pos_hint: {'center_x': 0.18,'center_y': 0.15}
 60              on_active: app.show_password(*args)
 61          
 62          MDLabel:
 63              id: password_text
 64              text: 'Show Password'
 65              pos_hint: {'center_x': .73, 'center_y': .15}
 66  ''')
 67  password().run()


What I have tried:

I have run the app and the following error incurred:

Traceback (most recent call last):
File "kivy\properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
     KeyError: 'password_login'
    
     During handling of the above exception, another exception occurred:
    
     Traceback (most recent call last):
       File "d:\Learning App\kivyMD\test.py", line 68, in <module>
         password().run()
       File "D:\Learning App\kivyMD\kivy_venv\lib\site-packages\kivy\app.py", line 955, in run
         runTouchApp()
       File "D:\Learning App\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 574, in runTouchApp
         EventLoop.mainloop()
       File "D:\Learning App\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 339, in mainloop
         self.idle()
       File "D:\Learning App\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 383, in idle
         self.dispatch_input()
       File "D:\Learning App\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 334, in dispatch_input
         self.active = True
       File "kivy\properties.pyx", line 520, in kivy.properties.Property.__set__
       File "kivy\properties.pyx", line 567, in kivy.properties.Property.set
       File "kivy\properties.pyx", line 606, in kivy.properties.Property._dispatch
       File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
       File "kivy\_event.pyx", line 1189, in kivy._event.EventObservers._dispatch
       File "D:\Learning App\kivyMD\kivy_venv\lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
         exec(__kvlang__.co_value, idmap)
       File "<string>", line 33, in <module>
       File "d:\Learning App\kivyMD\test.py", line 23, in show_password
         self.root.ids.password_login.password = False
       File "kivy\properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
     AttributeError: 'super' object has no attribute '__getattr__'
Posted
Updated 29-Sep-22 22:19pm
v2

1 solution

As far as I can make out from the error messages there is something wrong with the line:
Python
self.root.ids.password_login.password = False

The message
File "kivy\properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
     KeyError: 'password_login'

Suggests that your reference is incorrect, which is why it cannot find 'password_login'.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900