3

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

Akshat Zala
  • 710
  • 1
  • 8
  • 23
micma
  • 223
  • 2
  • 4
  • 16
  • 1
    You should show the relevant code in the button that causes the first error you gave. – SimonT Dec 28 '13 at 15:05
  • 1
    What SimonT says is absolutely correct. Some people have already probably guessed your problem, but in the future you should actually show the code you're running that raises the Exception. – DanielSank Dec 28 '13 at 15:09

3 Answers3

15

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)

m01
  • 9,033
  • 6
  • 32
  • 58
  • 3
    The difference in the error message is caused by the OP using Python 3, where `F.foo` returns a regular function. – Martijn Pieters Dec 28 '13 at 15:32
  • You can also run the program as F().foo() This is because in Python functions require () even if you aren't providing any arguments in the parentheses. – stidmatt Feb 25 '19 at 18:18
3

Did you create an instance of the Email_Stuff before calling method ? Because the self is the current object calling the method so it's needed.

class A():
    def __init__(self, attr):
        self.attr = attr
    def func(self):
        print(self.attr)

a = A(42)
a.func() # print 42
# or
A(32).func() # print 32
nobe4
  • 2,802
  • 3
  • 28
  • 54
0

Also You can avoid to write "self" in the function, as example:

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

>>> F.foo()

..: 1

Antonio Ramírez
  • 174
  • 1
  • 2
  • 10