[英]missing 1 required positional argument:'self'
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...我有一个连接到 Email_Stuff.From_Email 的按钮和一个连接到 Email_Stuff.From_Email 等的按钮...
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..但是如果我从 From_Email 和 To_Email 等中取出 selfs..
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我很抱歉这篇长文章
I think you're hitting the following problem.我认为您遇到了以下问题。 If you take the following class
F
:如果您参加以下课程
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()
,您应该会收到与您看到的类似的错误。
>>> 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
的对象上调用foo()
:
>>> 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...
我有一个连接到 Email_Stuff.From_Email 的按钮和一个连接到 Email_Stuff.From_Email 等的按钮...
You'll probably need to instantiate an object of Email_Stuff
, and then call yourobject.From_Email()
.您可能需要实例化一个
Email_Stuff
对象,然后调用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) (如果你的类
Email_Stuff
还包含 GUI 按钮处理程序的东西,你可以从按钮处理程序调用self.From_Email()
)
Did you create an instance of the Email_Stuff before calling method ?您是否在调用方法之前创建了 Email_Stuff 的实例? Because the self is the current object calling the method so it's needed.
因为 self 是调用该方法的当前对象,所以它是必需的。
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
Also You can avoid to write "self" in the function, as example:你也可以避免在函数中写“self”,例如:
>>> class F():
def foo():
return 1
>>> F.foo()
..: 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.