Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

missing 1 required positional argument:'self'

Tags:

python

here is my code:

class Email_Stuff():
    def __init__(self):
        self.emailaddr = None
        self.recipaddr = None
        self.EmailUser = None
        self.EmailPass = None
    def From_Email(self):
        self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
    def To_Email(self):
        self.recipaddr = turtle.textinput("Client Email", "What is your client's email address?")
    def Email_Username(self):
        self.EmailUser = turtle.textinput("Your Email Username", "What is your email username?")
    def Email_Password(self):
        self.EmailPass = turtle.textinput("Your Email Password", "What is your email Password?")
    def Send_Email(self):
        print (self.emailaddr) #these are here for me to see if it is the right input
        print(self.recipaddr)
        print(self.EmailUser)
        print(self.EmailPass)
        import smtplib
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.login((self.EmailUser),(self.EmailPass))
        self.message = "Python Test Email"
        server.sendmail(self.emailaddr,self.recipaddr,self.message)

I have a button connected to Email_Stuff.From_Email and a button connected to Email_Stuff.From_Email etc...

Whenever I press the button to open up the turtle window it gives me this error:

Exception in Tkinter callback
Traceback (most recent call last):
Fileline "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", 1475, in __call__
return self.func(*args)
TypeError: From_Email() missing 1 required positional argument: 'self'

But then if I take out the selfs from the From_Email and To_Email etc..

class Email_Stuff():
    def __init__(self):
        self.emailaddr = None
        self.recipaddr = None
        self.EmailUser = None
        self.EmailPass = None
    def From_Email():
        self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
    def To_Email():
        self.recipaddr = turtle.textinput("Client Email", "What is your client's email address?")
    def Email_Username():
        self.EmailUser = turtle.textinput("Your Email Username", "What is your email username?")
    def Email_Password():
        self.EmailPass = turtle.textinput("Your Email Password", "What is your email Password?")
    def Send_Email(self):
        print (self.emailaddr) #these are here for me to see if it is the right input
        print(self.recipaddr)
        print(self.EmailUser)
        print(self.EmailPass)
        import smtplib
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.login((self.EmailUser),(self.EmailPass))
        self.message = "Python Test Email"
        server.sendmail(self.emailaddr,self.recipaddr,self.message)

I get this error message (this isnt all of it):

    self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
    NameError: global name 'self' is not defined

here is the button code:

Email_Button = Button(root, text='Enter Your Email', command=Email_Stuff.From_Email)
Email_Button.pack()
Email_Button.place(x=250,y=210)

I'm sorry for the long post

like image 973
micma Avatar asked Dec 01 '22 17:12

micma


1 Answers

I think you're hitting the following problem. If you take the following class F:

class F():
     def foo(self):
         return 1

and try to call F.foo(), you should get an error similar to the one you're seeing.

>>> F.foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method foo() must be called with F instance as first argument (got nothing instead)

What you need to do, is call foo() on an object of F:

>>> f=F()
>>> f.foo()
1

I have a button connected to Email_Stuff.From_Email and a button connected to Email_Stuff.From_Email etc...

You'll probably need to instantiate an object of Email_Stuff, and then call yourobject.From_Email(). (If your class Email_Stuff also contains the GUI button handler stuff, you can just call self.From_Email() from the button handler)

like image 158
m01 Avatar answered Dec 26 '22 21:12

m01