'inserting line of code in the same module according to user input

I'm trying to insert new lines of codes in the same running module every time the user input something. I tried with open function but this function in exe program will open a new file to write the user input in it and that will not help me. basically, I want user input to be added as a line of code in the same module that the application is running on. "note: Addingpage is another module that I use to add user input in it" here is my application:

`    def addingfunction(self):
        Name = self.name_add.get()
        Email = self.email_add.get()
        Password = self.passowrd_add.get()
        if " " in Name:
            self.name_fill_error = Label(self.adding_frame, text= "Name shouldn't have a space", fg='red', bg='white', font=('merriweather', 10, 'bold'))
            self.name_fill_error.grid(row=11, column=1, sticky='w')
            self.adding_frame.after(2000, self.name_fill_error.grid_remove)
        elif Name == "" or Email == "" or Password == "":
            self.name_fill_error = Label(self.adding_frame, text='Data is empty', fg='red', bg='white', font=('merriweather', 10, 'bold'))
            self.name_fill_error.grid(row=11, column=1, sticky='w')
            self.adding_frame.after(2000, self.name_fill_error.grid_remove)
        elif Name in Addingpage.accountslist:
            self.name_fill_error = Label(self.adding_frame, text='Account name already exist', fg='red', bg='white', font=('merriweather', 10, 'bold'))
            self.name_fill_error.grid(row=11, column=1, sticky='w')
            self.adding_frame.after(2000, self.name_fill_error.grid_remove)
        else:
            self.x += 1
            with open("Addingpage.py", 'a') as A:
                A.write(f"        self.e{Name} =  Frame(self.bigframe_1, bg='white')\n")
                A.write(
                    f"        Label(self.e{Name}, text='Account Name: {Name}', font=('merriweather', 10, 'bold'), bg='white', fg='blue').pack(anchor=W)\n")
                A.write(
                    f"        Label(self.e{Name}, text='Email: {Email}', font=('merriweather', 10, 'bold'), bg='white', fg='black').pack(anchor=W)\n")
                A.write(
                    f"        Label(self.e{Name}, text='Password: {Password}', font=('merriweather', 10, 'bold'), bg='white', fg='black').pack(anchor=W)\n")
                A.write(
                    f"        Label(self.e{Name}, text='', font=('merriweather', 10, 'bold'), bg='white', fg='red').pack(anchor=W)\n")
                A.write(f"        if '{Name}' in accountslist:\n            pass\n        else:\n            accountslist.append('{Name}')\n        self.e{Name}.pack(anchor=W)\n")
                self.clearingfunction()
                self.account_added = Label(self.adding_frame, text='Your account is added', fg='green', bg='white',
                                             font=('merriweather', 10, 'bold'))
                self.account_added.grid(row=11, column=1, sticky='w')
                self.adding_frame.after(2000, self.account_added.grid_remove)

    def addingpage(self):
        self.name_add = StringVar()
        self.email_add = StringVar()
        self.passowrd_add = StringVar()
        self.removeall()
        self.adding_frame = Frame(self.root, bg='white')
        self.welcome = Label(self.adding_frame, text='', width=15, bg='white').grid(row=0, rowspan=5, column=0)
        self.icongrid = Label(self.adding_frame, image=self.icon, bg='black', fg='white').grid(row=0, column=1)
        '''Name box'''
        Label(self.adding_frame, text='Name: ', bg='white', fg='black', font=('merriweather', 10, 'bold')).grid(row=6, column=0, sticky='e')
        self.name_add = Entry(self.adding_frame, font=('merriweather', 10, 'bold'), fg='blue', bg='gray', width=16)
        self.name_add.grid(row=6, column=1, sticky='w')
        '''Email box'''
        Label(self.adding_frame, text='E-mail: ', bg='white', fg='black', font=('merriweather', 10, 'bold')).grid(row=7, column=0, sticky='e')
        self.email_add = Entry(self.adding_frame, font=('merriweather', 10, 'bold'), fg='blue', bg='gray', width=32)
        self.email_add.grid(row=7, column=1)
        '''Password box'''
        Label(self.adding_frame, text='Password: ', bg='white', fg='black', font=('merriweather', 10, 'bold')).grid(row=8, column=0, sticky='e')
        self.passowrd_add = Entry(self.adding_frame, font=('merriweather', 10, 'bold'), fg='blue', bg='gray', width=32)
        self.passowrd_add.grid(row=8, column=1)
        '''Add button'''
        add_button_adding = Button(self.adding_frame, text='Add', fg='white', font=('merriweather', 9, 'bold'), bg='black', activeforeground='green', width=20, command= self.addingfunction).grid(row=9, column=1)
        '''Back to menu button'''
        self.back_to_menu = Button(self.adding_frame, text='Back to menu', font=('merriweather', 9, 'bold'), command=self.mainmenu, fg='white', bg='black', width=10).grid(row=10, column=1)
        self.adding_frame.grid(row=0, column=0)
`


Solution 1:[1]

I used in the end SQLite database where I store information and retrieve them from there, I tried JSON file but it is not good for my purpose, even SQLite I had to manipulate and play around with it in order to make it function as I want. Here is the code at the end.

for i in range(1, 1000):
    c.execute(f"SELECT name FROM data WHERE id='{i}'")
    name = str(c.fetchone())
    end_slice = slice(-3)
    Name = name[end_slice][2:]
    c.execute(f"SELECT email FROM data WHERE id='{i}'")
    email = str(c.fetchone())
    Email = email[end_slice][2:]
    c.execute(f"SELECT password FROM data WHERE id='{i}'")
    password = str(c.fetchone())
    Password = password[end_slice][2:]
    if Name == "":
        continue
    Label(self.bigframe_1, text=f'Account Name: {Name}', font=('merriweather', 10, 'bold'), bg='white',
              fg='blue').pack(anchor=W)
    Label(self.bigframe_1, text=f'Email: {Email}', font=('merriweather', 10, 'bold'),
              bg='white', fg='black').pack(anchor=W)
    Label(self.bigframe_1, text=f'Password: {Password}', font=('merriweather', 10, 'bold'), bg='white',
              fg='black').pack(anchor=W)
    Label(self.bigframe_1, text='', font=('merriweather', 10, 'bold'), bg='white', fg='red').pack(anchor=W)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elias