2

what is wrong with my code???

login.py

class NewApp(MDApp):
    def build(self):
        self.theme_cls.theme_style="Dark"
        self.theme_cls.primary_palette="Blue"
        self.strng = Builder.load_string(help_str)

i am trying to call this function on the press of the submit button.the function is getting called yet its showing an attribute error

def check_admin(self):
        

        self.id=self.strng.get_screen('adminloginpage').ids.adminid.text

      self.adpassword=self.strng.get_screen('adminloginpage').ids.adminpassword.text

the kv code

help_str='''
 <AdminloginPage>:
name:'adminloginpage'

BoxLayout:
orientation:'vertical'
size:root.width,root.height
MDLabel:
text:"Admin screen"
font_style:"H4"
halign:'center'
pos_hint:{'center_y':0.9}
size_hint:(0.7,0.1)
MDTextField:
id:'adminid'
halign:'center'
hint_text:'enter admin id '
icon_right:'account'
pos_hint:{'center_x':0.5,'center_y':0.5}
size_hint:(0.7,0.1)
helper_text:'Required'
helper_text_mode:'on_error'
required:True
multiline:False

MDTextField:
id:'adminpassword'
halign:'center'
hint_text:'enter admin password '
icon_right:'eye-off'
pos_hint:{'center_x':0.5,'center_y':0.5}
size_hint:(0.7,0.1)
helper_text:'Required'
helper_text_mode:'on_error'
required:True
password:True
multiline:False

MDTextButton:
text: 'submit'
md_bg_color:app.theme_cls.primary_color
user_font_size:'30sp'
size_hint:(0.9,0.5)
on_press:app.check_admin()'''

i am trying to store the userid and password to validate

  • there isn't enough code here to reproduce this error. it is possible to put runnable code within one code block so you should try to do that to make a minimal runnable example. in the kivy file, ids do not have quotes. id:'adminpassword' should be id: adminpassword. your build() method should return a top-level widget using the return keyword. try this line: print(type(self.strng)) and this: return self.strng – Mark Jan 02 '23 at 12:48

1 Answers1

1

the code below is runnable and does not produce the error you listed. An assumption was made that you wanted a ScreenManager so this was installed as the top level widget and just one screen is loaded according to your kivy string. The layout isn't polished but the app should load with the elements present and you can click the button and it will print the password to the console.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from kivy.lang import Builder

# kivymd
from kivymd.app import MDApp
from kivymd.uix.screenmanager import ScreenManager
from kivymd.uix.screen import MDScreen

help_str='''
<AdminloginPage>:
    name:'adminloginpage'
    
    BoxLayout:
        orientation:'vertical'
        size:root.width,root.height
        MDLabel:
            text:"Admin screen"
            font_style:"H4"
            halign:'center'
            pos_hint:{'center_y':0.9}
            size_hint:(0.7,0.1)
        MDTextField:
            id: adminid
            halign:'center'
            hint_text:'enter admin id '
            icon_right:'account'
            pos_hint:{'center_x':0.5,'center_y':0.5}
            size_hint:(0.7,0.1)
            helper_text:'Required'
            helper_text_mode:'on_error'
            required:True
            multiline:False
        
        MDTextField:
            id: adminpassword
            halign:'center'
            hint_text:'enter admin password '
            icon_right:'eye-off'
            pos_hint:{'center_x':0.5,'center_y':0.5}
            size_hint: 0.7, 0.1
            helper_text:'Required'
            helper_text_mode:'on_error'
            required:True
            password:True
            multiline:False
        
        MDTextButton:
            text: 'submit'
            md_bg_color:app.theme_cls.primary_color
            user_font_size:'30sp'
            size_hint: 0.9, 0.5
            on_press:app.check_admin(self)
'''


class AdminloginPage(MDScreen):
    def __init__(self, **kw):
        super().__init__(**kw)


class NewApp(MDApp):

    def check_admin(self, my_button, *args, **kwargs):
        self.id = self.top_level_widget.get_screen('adminloginpage').ids.adminid.text
        self.adpassword = self.top_level_widget.get_screen('adminloginpage').ids.adminpassword.text
        print(f"password: '{self.adpassword}'\napp: {self}\nbutton: {my_button}")

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.top_level_widget = ScreenManager()
        self.login_screen = AdminloginPage(name='adminloginpage')

    def build(self):
        self.theme_cls.theme_style="Dark"
        self.theme_cls.primary_palette="Blue"
        self.top_level_widget.add_widget(self.login_screen)
        print(type(self.top_level_widget))
        return self.top_level_widget


if __name__ == '__main__':
    Builder.load_string(help_str)
    my = NewApp()
    my.run()
Mark
  • 532
  • 1
  • 4
  • 9