Jean-Sebastien
Jean-Sebastien

Reputation: 31

Saving username and password in a Python/Kivy app

I added a checkbox on my sign-in screen for saving the username and password information so the user will not have to enter them every time. I thought about creating a text file that will store this information, but maybe there is a better approach. I'm not able to save it properly; this is what I have so far. In the init method of my class, I'm checking to see if there is a text file with the information. If so, I want to extract the username and password to fill in the TextInputs on my screen. If not, I'll leave them empty and let the user fill in the two TextInputs. The Textinputs are taking care of in my next method add_user(). I get this error: AttributeError: 'super' object has no attribute '__getattr__'. I haven't figured out the checkbox behavior since I already an error. Does anyone have an idea?

try.py
class SigninWindows(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        if os.path.isfile('prev_details.txt'):
            with open('prev_details.txt', 'r') as f:
                d = f.read().split(',')
                self.ids.username_field.text = d[0]
                self.ids.pwd_field.text = d[1]

        else:
            self.ids.username_field.text = ''
            self.ids.pwd_field.text = ''


    def add_user(self):
        uname = self.ids.username_field.text
        passw = self.ids.pwd_field.text
        info = self.ids.info
        table_name = uname.replace('@', '_').replace('.', '_')
try.kv

<SigninWindows>:
    id: signin_page
    name: "signin_page"
    orientation: "vertical"
    spacing: 10
    space_x: self.size[0]/5.5
    canvas.before:
        Color:
            rgba: (0,0,0,1)
        Rectangle:
            size: self.size
            pos: self.pos

    BoxLayout:
        id: data_signin
        orientation: 'vertical'
        size_hint_x: 1
        BoxLayout:
            Image:
                id: ds_im
                orientation: 'vertical'
                source: 'ds.png'
                allow_stretch: True
        BoxLayout:
            id: validate_info
            orientation: "vertical"
            size_hint: 1,0.8
            padding: 80, 10
            Label:
                id: info
                text: ''
                markup: True
            TextInput:
                id: username_field
                text: ''
                hint_text: "Username"
                hint_text_color: 0.5,0.5,0.5,1
                multiline: False
                focus: True
                on_text_validate: pwd_field.focus = True
                size_hint: 1, .8
                foreground_color: 0.5,0.5,0.5,1
                background_color: .1,.1,.1,1
                write_tab: False
            TextInput:
                id: pwd_field
                text: ''
                hint_text: "Password"
                hint_text_color: 0.5,0.5,0.5,1
                multiline: False
                password: True
                on_text_validate: root.validate_user()
                size_hint: 1,0.8
                foreground_color: 0.5,0.5,0.5,1
                background_color: .1,.1,.1,1
        BoxLayout:
            id: remember_section
            orientation : 'horizontal'
            size_hint_x: None
            width: 80
            size_hint_y: None
            height: 50
            padding: 80, 10
            CheckBox:
                text: ''
                size_hint_x: None
                width: 25
                size_hint_y: None
                height: 25
                canvas.before:
                    Color:
                        rgba: (1, 1, 1, 1)
                    Rectangle:
                        size: self.size
                        pos: self.pos
            Label:
                text: 'Remember User id?'
                font_size: 20
                size_hint_x: None
                width: 220

Upvotes: 0

Views: 631

Answers (2)

Draxdo
Draxdo

Reputation: 5

There is not a much better way to do this rather than a text file. Import the OS module and do os.readfile(filepath and or if it is in the same folder as your .py file just the name) and read from there the rest is straight forward.

Upvotes: 0

John Anderson
John Anderson

Reputation: 38857

The ids dictionary of SigninWindows has not yet been created when the __init__() method is running. You can move that code into another method and call it using Clock.schedule_once(). That will delay its execution very slightly until the ids are available.

Upvotes: 1

Related Questions