这是我的代码:
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)
我有一个连接到Email_Stuff.From_Email的按钮和一个连接到Email_Stuff.From_Email等的按钮......
每当我按下按钮打开龟窗时,它就会给我这个错误:
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'
但是如果我从From_Email和To_Email等中取出自我。
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)
我收到此错误消息(这不是全部):
self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
NameError: global name 'self' is not defined
这是按钮代码:
Email_Button = Button(root, text='Enter Your Email', command=Email_Stuff.From_Email)
Email_Button.pack()
Email_Button.place(x=250,y=210)
请帮忙!对不起,我很抱歉
答案 0 :(得分:9)
我认为你遇到了以下问题。如果您参加以下课程F
:
class F():
def foo(self):
return 1
并尝试拨打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)
您需要做的是,在foo()
的对象上致电F
:
>>> f=F()
>>> f.foo()
1
我有一个连接到Email_Stuff.From_Email的按钮和一个按钮 连接到Email_Stuff.From_Email等......
您可能需要实例化Email_Stuff
的对象,然后调用yourobject.From_Email()
。 (如果您的班级Email_Stuff
也包含GUI按钮处理程序内容,您只需从按钮处理程序中调用self.From_Email()
答案 1 :(得分:3)
在调用方法之前,您是否创建了Email_Stuff的实例?因为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
答案 2 :(得分:0)
还可以避免在函数中写“ self”,例如:
>>> class F():
def foo():
return 1
>>> F.foo()
..: 1