全局和局部Python
我正试图通过Tkinter发送一条短信。所以你输入了全局和局部Python,python,python-3.x,tkinter,global,Python,Python 3.x,Tkinter,Global,我正试图通过Tkinter发送一条短信。所以你输入了sms:hello。它会发送一条短信,说你好。为此,它使用AT&T电子邮件服务器和GMail向word发送电子邮件。因此,程序读取INFO.txt,其中包含所有电子邮件身份验证g_用户g_密码和m_数值。然后,它使用这些来发送一封发送文本消息的电子邮件 现在我的问题是UnboundLocalError:在赋值之前引用了局部变量“g_user”。我知道这是由非全局变量引起的。有人能帮我吗?我被难住了 root = Tk() #open file
sms:hello
。它会发送一条短信,说你好
。为此,它使用AT&T电子邮件服务器和GMail向word发送电子邮件。因此,程序读取INFO.txt
,其中包含所有电子邮件身份验证g_用户
g_密码
和m_数值
。然后,它使用这些来发送一封发送文本消息的电子邮件
现在我的问题是UnboundLocalError:在赋值之前引用了局部变量“g_user”
。我知道这是由非全局变量引起的。有人能帮我吗?我被难住了
root = Tk()
#open file
file=open('INFO.txt')
line=file.readline()
if 'Mobile_number:::' in line:
m_num=line[16:]
if 'GMail_name:::' in line:
g_user=line[13:]
if 'GMail_pass:::' in line:
g_pass=line[13:]
def callback(event):
text = inputfield.get()
if 'sms:' in text:
textmessage()
def textmessage():#sms:
import smtplib
#open file
file=open('INFO.txt')
line=file.readline()
if 'Mobile_number:::' in line:
m_num=line[16:]
if 'GMail_name:::' in line:
g_user=line[13:]
if 'GMail_pass:::' in line:
g_pass=line[13:]
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = '{}@gmail.com'.format(g_user)
password='{}'.format(g_pass)
recipient = '{}@txt.att.net'.format(m_num)
subject = 'Gmail SMTP Test'
body = text[4:]
"Sends an e-mail to the specified recipient."
body = "" + body + ""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
text2=text[4:]
confirmation="SMS containing '{}' sent".format(text2)
tex.insert(END,confirmation)
tex=Text(root)
tex.pack(side='right')
inputfield = Entry(root)
inputfield.pack(side='bottom')
inputfield.bind('<Return>', callback)
root.mainloop()
root=Tk()
#打开文件
文件=打开('INFO.txt')
line=file.readline()
如果行中有“手机号码:::”:
m_num=行[16:]
如果行中有“GMail_name:::”:
g_用户=行[13:]
如果行中有“GMail\u密码”:
g_pass=行[13:]
def回调(事件):
text=inputfield.get()
如果文本中有“sms:”:
textmessage()
def textmessage():#短信:
导入smtplib
#打开文件
文件=打开('INFO.txt')
line=file.readline()
如果行中有“手机号码:::”:
m_num=行[16:]
如果行中有“GMail_name:::”:
g_用户=行[13:]
如果行中有“GMail\u密码”:
g_pass=行[13:]
SMTP_服务器='SMTP.gmail.com'
SMTP_端口=587
发件人=“{}@gmail.com”。格式(g_用户)
密码=“{}”。格式(g_pass)
收件人=“{}@txt.att.net”。格式(m_num)
主题='Gmail SMTP测试'
正文=文本[4:]
“向指定的收件人发送电子邮件。”
body=”“+body+“”
标题=[“发件人:”+发件人,
“主题:”+主题,
“致:“+收件人,
“MIME版本:1.0”,
“内容类型:文本/html”]
headers=“\r\n”。加入(头)
session=smtplib.SMTP(SMTP\u服务器、SMTP\u端口)
session.ehlo()
session.starttls()
session.ehlo
会话.登录(发件人,密码)
session.sendmail(发件人、收件人、标题+“\r\n\r\n”+正文)
session.quit()
text2=文本[4:]
确认=“已发送包含“{}”的短信”。格式(text2)
文本插入(结束,确认)
tex=文本(根)
tex.pack(side='right')
inputfield=条目(根)
inputfield.pack(side='bottom')
inputfield.bind(“”,回调)
root.mainloop()
这一行最有可能出现问题:
sender = '{}@gmail.com'.format(g_user)
因为
if
语句条件(if'GMail\u name::'in line
)的计算结果为False
,因此您的g\u user
变量从未在该函数的本地范围内定义。仔细查看错误消息:
UnboundLocalError: local variable 'g_user' referenced before assignment
一个很好的经验法则是假设错误消息是真实的。在这种情况下,它告诉您两个非常重要的细节:
- 它认为
是一个局部变量g_user
- 它认为
在设置之前就被使用了g_user
[13://code>,如果两个值不同,并且两个值在同一行中,这是不可能的
因为您刚刚开始学习编程,所以不要只是将代码行扔进一个文件并希望它们能够工作,也不要让其他人来解决您的问题。想想电脑在做什么。循序渐进地完成代码,问题就会不言而喻