Enwerem Melvin
Enwerem Melvin

Reputation: 36

How to get user input from an MDTextField in KivyMD

Please I keep getting an attribute error when I try to get the text a user input into an MDTextField, tried everything I could think of, researched and couldn't get anything. Please I need your help.

I try to get the text with this argument of self.root.ids.MyId.text

This is the error I get:

File "C:/Users/user/Documents/KmD/Webma/main_webma.py", line 120, in login
     email_addr = self.root.ids.email.text
   File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

And this is my main.py code

from kivymd.app import MDApp
from kivy.clock import Clock
from kivy.lang import Builder
from home_page_strings import toolbar
import sqlite3
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.picker import MDThemePicker
from kivy.uix.popup import Popup
from kivymd.uix.textfield import MDTextField


# calling of screen classes and addition to the screen_manager

class Friend1(Screen):
    pass


class WelcomeScreen(Screen):
    def __init__(self, **kwargs):
        super(WelcomeScreen, self).__init__(**kwargs)
        Clock.schedule_once(self.callNext, 5)

    def callNext(self, dt):
        self.manager.current = 'account'


class AccountScreen(Screen):
    pass


class HomeScreen(Screen):
    pass


class RoomScreen(Screen):
    pass


class ContactsScreen(Screen):
    pass


class CallsScreen(Screen):
    pass


class SettingsScreen(Screen):
    pass


class FaqScreen(Screen):
    pass


class InviteScreen(Screen):
    pass


manager = ScreenManager()
manager.add_widget(Friend1(name='friend1'))
manager.add_widget(HomeScreen(name='home'))
manager.add_widget(WelcomeScreen(name='welcome'))
manager.add_widget(RoomScreen(name='room'))
manager.add_widget(ContactsScreen(name='contacts'))
manager.add_widget(CallsScreen(name='calls'))
manager.add_widget(SettingsScreen(name='settings'))
manager.add_widget(FaqScreen(name='faq'))
manager.add_widget(InviteScreen(name='invite'))
manager.add_widget(AccountScreen(name='account'))


# end of call of screen classes and addition to screenmanager


class EMC_WebmaApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    # -------------------------- CODE FOR THE DATABASE ---------------------------------- #
    conn = sqlite3.connect('webma_database.db')
    cursor = conn.cursor()

    # cursor.execute('''CREATE TABLE users (
    #                 email text,
    #                 password integer
    #                 )''')

    def sign_up(self, cursor):
        cursor.execute('INSERT INTO users VALUES('', '')')

    conn.commit()
    conn.close()

    # -------------------------- END OF CODE FOR THE DATABASE ---------------------------------- #

    # BUILDER FUNCTION AND SCREEN
    def build(self):
        screen = Screen()

        # theme settings
        self.theme_cls.primary_palette = 'DeepPurple'
        self.theme_cls.theme_style = 'Dark'
        # end of theme declaration

        # loaded strings
        navigation_bar = Builder.load_string(toolbar)
        # end of loaded strings

        # addition of loaded strings to screen + return screen
        screen.add_widget(navigation_bar)

        return screen
        # end of addition

    # END OF BUILDER FUNCTION

    # USER VALIDATION FUNCTION

    def login(self):
        email_addr = self.root.ids.email.text

        if email_addr == str(''):
            print('Invalid Email Address')

    # END OF USER VALIDATION

    def change(self, dt):
        self.root.manager.current = 'home'

    # App theme manager
    def change_theme(self):
        picker = MDThemePicker()
        picker.open()

    # End of app theme manager

    # home page search bar
    def search_bar(self, dt):
        searchbar = Popup(
            title='Search',
            content=MDTextField(
                helper_text='Input values to be searched for',
                helper_text_mode='on_focus'
            ),
            auto_dismiss=True,
            # size_hint=(None, None),
            # size=(400, 98)
            size_hint=(0.7, 0.17),
            pos_hint={'center_x': 0.5, 'center_y': 0.8}
        )
        searchbar.open()


# APP RUN
if __name__ == '__main__':
    EMC_WebmaApp().run()
# WELL... THIS IS THE END OF EVERYTHING

Then my string code where I try to get my text from

    toolbar = '''
ScreenManager:
    WelcomeScreen:
    HomeScreen:
    RoomScreen:
    ContactsScreen
    CallsScreen:
    SettingsScreen:
    FaqScreen:
    InviteScreen:
    AccountScreen:
    Friend1:
    
<WelcomeScreen>:
    name: 'welcome'
    Image:
        source: 'logo.png'
        size_hint: 0.4, 0.4
        pos_hint: {'center_x': 0.5, 'center_y':0.6}
        
    MDLabel:
        text: 'Webma'
        halign: 'center'
        font_style: 'H3'
        color: (1, 1, 1, 1)
        pos_hint: {'center_x': 0.5, 'center_y':0.8}
        
    MDLabel:
        text: 'Powered by emc_codes'
        halign: 'center'
        bold: 'True'
        italic: True
        pos_hint: {'center_x': 0.5, 'center_y':0.1}
    
<HomeScreen>:
    name: 'home'
    
    NavigationLayout:
        ScreenManager:
            Screen:                        
                BoxLayout:               
                    orientation: 'vertical'
                    spacing: '8dp'

                    MDToolbar:
                        title: 'EMC_Webma'
                        left_action_items: [['menu', lambda x: navigation.set_state('open')]]
                        right_action_items: [['card-search', app.search_bar]]
                        elevation: 10
                        
                    ScrollView:
                        MDList:
                            TwoLineAvatarIconListItem:
                                text: 'Friend 1'
                                ImageLeftWidget: 
                                    source: 'einstein.jpg'
                                IconRightWidget:    
                                    icon: 'dots-vertical'
                
        MDNavigationDrawer:
            id: navigation
            
            BoxLayout:
                orientation: 'vertical'
                spacing: '8dp'
                padding: '8dp'
                            
                Image:
                    source: 'einstein.jpg'
                    
                MDLabel:
                    text: 'Enwerem Melvin Confidence'
                    font_style: 'Subtitle2'
                    size_hint_y: None
                    height: self.texture_size[1]
                    
                MDLabel:
                    text: '[email protected]'
                    font_style: 'Caption'
                    size_hint_y: None
                    height: self.texture_size[1]
                                    
                ScrollView:
                    MDList:
                        OneLineIconListItem:
                            text: 'Room'
                            on_release: root.manager.current = 'room'
                            IconLeftWidget:
                                icon: 'theater'
                                on_release: root.manager.current = 'room'
                                
                        OneLineIconListItem:
                            text: 'Contacts'
                            on_release: root.manager.current = 'contacts'
                            IconLeftWidget:
                                icon: 'contacts'
                                on_release: root.manager.current = 'contacts'
                                
                        OneLineIconListItem:
                            text: 'Calls'
                            on_release: root.manager.current = 'calls'
                            IconLeftWidget:
                                icon: 'cellphone-android'
                                on_release: root.manager.current = 'calls'
                                
                        OneLineIconListItem:
                            text: 'Settings'
                            on_release: root.manager.current = 'settings'
                            IconLeftWidget:
                                icon: 'settings'
                                on_release: root.manager.current = 'settings'
                                
                        OneLineIconListItem:
                            text: 'FAQ'
                            on_release: root.manager.current = 'faq'
                            IconLeftWidget:
                                icon: 'frequently-asked-questions'
                                on_release: root.manager.current = 'faq'
                                
                        OneLineIconListItem:
                            text: 'Invite Friends'
                            on_release: root.manager.current = 'invite'
                            IconLeftWidget:
                                icon: 'account-plus'
                                on_release: root.manager.current = 'invite'
                                
                        OneLineIconListItem:
                            text: 'Theme Manager'
                            on_release:
                                app.change_theme()
                            IconLeftWidget:
                                icon: 'theme-light-dark'

<RoomScreen>:
    name: 'room'
    
    MDLabel:
        text: 'Welcome to the Room Screen'
        halign: 'center'
        bold: True
        font_size: 50
    MDIconButton:
        icon: 'keyboard-backspace'
        pos_hint: {'center_x': 0.06, 'center_y': 0.96}
        on_release: root.manager.current = 'home'
        size_hint: None, None
        
<ContactsScreen>:
    name: 'contacts'
    
    MDLabel:
        text: 'Welcome to the Contacts Screen'
        halign: 'center'
        bold: True
        font_size: 50
    MDIconButton:
        icon: 'keyboard-backspace'
        pos_hint: {'center_x': 0.06, 'center_y': 0.96}
        on_release: root.manager.current = 'home'
        size_hint: None, None
        
<CallsScreen>:
    name: 'calls'
    
    MDLabel:
        text: 'Welcome to the Calls Screen'
        halign: 'center'
        bold: True
        font_size: 50
    MDIconButton:
        icon: 'keyboard-backspace'
        pos_hint: {'center_x': 0.06, 'center_y': 0.96}
        on_release: root.manager.current = 'home'
        size_hint: None, None
        
<SettingsScreen>:
    name: 'settings'
    
    MDLabel:
        text: 'Welcome to the Settings Screen'
        halign: 'center'
        bold: True
        font_size: 50
    MDIconButton:
        icon: 'keyboard-backspace'
        pos_hint: {'center_x': 0.06, 'center_y': 0.96}
        on_release: root.manager.current = 'home'
        size_hint: None, None
        
<FaqScreen>:
    name: 'faq'
    
    MDLabel:
        text: 'Welcome to the Faq Screen'
        halign: 'center'
        bold: True
        font_size: 50
    MDIconButton:
        icon: 'keyboard-backspace'
        pos_hint: {'center_x': 0.06, 'center_y': 0.96}
        on_release: root.manager.current = 'home'
        size_hint: None, None
        
<InviteScreen>:
    name: 'invite'
    
    MDLabel:
        text: 'Welcome to the Invite Screen'
        halign: 'center'
        bold: True
        font_size: 50
    MDIconButton:
        icon: 'keyboard-backspace'
        pos_hint: {'center_x': 0.06, 'center_y': 0.96}
        on_release: root.manager.current = 'home'
        size_hint: None, None
        
<AccountScreen>:
    name: 'account'
    
    BoxLayout:
    
    MDLabel:
        text: 'Account Login'
        pos_hint: {'center_x': 0.6, 'center_y': 0.9}
        color: (1, 1, 1, 1)
        font_size: 40
            
    MDTextFieldRound: 
        id: email
        hint_text: 'Email Address'
        icon_right: 'email'
        icon_right_color: app.theme_cls.primary_color
        pos_hint: {'center_x': 0.5, 'center_y': 0.7}
        size_hint_x: None
        width: 300
    
    MDTextFieldRound: 
        id: password
        hint_text: 'Enter Password'
        icon_right: 'lock'
        icon_right_color: app.theme_cls.primary_color
        password: True
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        size_hint_x: None
        width: 300
        
    MDRaisedButton:
        text: 'Login'
        md_bg_color: app.theme_cls.primary_color
        pos_hint: {'center_x': 0.5, 'center_y': 0.2}
        on_release: app.login()       # root.manager.current = 'home'
        
<Friend1>
    name: 'friend1'
    
    BoxLayout:
        orientation: 'vertical'
        
        MDToolbar:
            title: 'Friend 1'
            left_action_items: [['keyboard-backspace', app.change]]
            right_action_items: [['phone', lambda x: print('go ahead and make a call')], ['video', lambda x: print('Video')], ['dots-vertical', lambda x: print('dots')]]

        MDLabel:
            text: 'This is a test screen for our Chat'
            halign: 'center'
        
'''

Please guys I really appreciate the help. Thanks in advance...

Upvotes: 0

Views: 3304

Answers (2)

Asher Manangan
Asher Manangan

Reputation: 90

value = self.root.get_screen('nameofscreen').ids.username.text
print("What you typed is: ", value)
syntax = self.root.get_screen('name').ids.id.text

Upvotes: 0

John Anderson
John Anderson

Reputation: 39082

The email id only appears in the ids for the AccountScreen. Any id appearing in a kv gets inserted into the ids of the widget that is the root of the rule that contains the id. So, you must access the AccountScreen instance to get to the email id:

# BUILDER FUNCTION AND SCREEN
def build(self):
    screen = Screen()

    # theme settings
    self.theme_cls.primary_palette = 'DeepPurple'
    self.theme_cls.theme_style = 'Dark'
    # end of theme declaration

    # loaded strings
    self.navigation_bar = Builder.load_string(toolbar)  # save a reference to the loaded ScreenManager
    # end of loaded strings

    # addition of loaded strings to screen + return screen
    screen.add_widget(self.navigation_bar)

    return screen
    # end of addition

# END OF BUILDER FUNCTION

# USER VALIDATION FUNCTION

def login(self):
    # use the saved reference to access the email address
    email_addr = self.navigation_bar.get_screen('account').ids.email.text

    if email_addr == str(''):
        print('Invalid Email Address')

# END OF USER VALIDATION

Upvotes: 1

Related Questions