dfgdf dzfgdf
dfgdf dzfgdf

Reputation: 11

trying to import a file of python (where I have the functions) into my main python file

hello as i said in my question i am trying to import a file of python (where I have the functions) into my main python file, the file imports however, it gives me an error saying "accounts" is not defined (which is a list I made). I assume this happens because the main file has it defined, but the function file doesn't?, same thing happens when i try to get the user's input with .get() ( .get() is used in the function file whereas the actual entry box is in the main file so it says AttributeError: 'final_cart' object has no attribute 'email') (final cart is a class). how can i fix these errors and run the code, without trying to for example defining the list in the "functioon" file (because I have already defined them in the main file).

here is some of the main code from my main file

import functioon




class Goode_brothers:


    def __init__(self, parent):

   
       self.my_frame = Frame(parent)
       self.my_frame.pack()

   
       self.background = Image.open('images\\food.jpg')
       self.background_image = ImageTk.PhotoImage(self.background)
       self.img = Label(parent, image = self.background_image)
       self.img.place(x = -26, y =0)

    
       self.img_login = PhotoImage(file = 'new-dip-project\\button (3).png')
       self.login_button = Button(parent,image = self.img_login, command = functioon.read_info(self), bd = BORDER, cursor = CURSOR, bg = '#3b353b', activebackground = '#3b353b')
       self.login_button.place(x = 275, y = 340)



    def create_pass(self):

    
       self.password_length = Label(self.root2, text = '')
       self.password_length.place(x = 80, y = 140)

    
       self.pass_word = str(self.password2.get())

    
       if len(self.pass_word) >= 8:
           functioon.save_info(self)
           self.registered = Label(self.root2, text = 'You have successfully registered, this window will now automatically close', font=("open sans", "8"))
           self.registered.place(x = 80, y = 140)
       
           self.root2.after(1500, self.root2.destroy)
       else:
        
           self.password_length.configure(text="""Your password must be atleast eight characters long. Please try again""", font=("open sans", "8"))

code from functions file

from tkinter import*
from PIL import Image, ImageTk


  
def save_info(self):

   
    self.email_reg = str(self.email2.get())
    self.pass_word = str(self.password2.get())
    print(self.email2)
    
    file = open('emails.txt', 'a+')
    ## writing the user credentials first email and the password, seprerating with a comma
    file.write(self.email_reg + ', ' + self.pass_word + '\n')


    
def read_info(self):

    
    with open("emails.txt") as read_ep:
        for line in read_ep:
            
            accounts.append(line.strip().split(", "))
    
    credential = [self.email.get(), self.password.get()]
    
    if credential in accounts:
        self.open_menu()
    else:
        
        self.ep_notexist = Label(root, text = "Your Email or Password is incorrect, Please try again", font=("open sans", "8"))
        self.ep_notexist.place(x = 210, y = 300)
        self.ep_notexist.after(4000, self.ep_notexist.destroy)
        self.email.delete(0, END)
        self.password.delete(0, END)

Upvotes: 0

Views: 60

Answers (3)

LAKSHYA SINGH CHAUHAN
LAKSHYA SINGH CHAUHAN

Reputation: 21

To import a python file into your main file:

import #(file name)

or if you want to import a function from a python file to the main file:

from """file name remove the ". """ import #function to import from the file

Please note that the file you are importing from should be in the same location as you main file.

Upvotes: 1

Kroshtan
Kroshtan

Reputation: 677

To use a variable from the main part of your code in the function defined in functioon.py, you will have to pass the variable to the function, similar to functions within the same file.

def read_info(self, accounts):
    etc.

Upvotes: 0

Rory LM
Rory LM

Reputation: 180

Because they are defined in another file, save_info & read_info are not class methods of the Goode_brothers class.

Instead you are passing the object (self) into save_info & read_info then referencing email and other attributes that don't exist in the Goode_brothers class.

If you must do it like this, define them first before referencing:

class Goode_brothers:

    email = ""

    def __init__(self, parent):
        email = "default"

Also I recommend renaming the method argument in functioon from self to something else to avoid confusion.

def read_info(goode_brothers_object):
    goode_brothers_object.email = "new email"

These functions should really be a part of the Goode_brothers class.

There might be other issues as well

Upvotes: 0

Related Questions