【问题标题】:Global and Local Python全局和本地 Python
【发布时间】:2013-06-23 16:31:58
【问题描述】:

我正在尝试通过 Tkinter 发送短信。所以你输入sms:hello。这会发送一条短信,上面写着hello。为此,它使用 AT&T 电子邮件服务器和 GMail 通过电子邮件发送单词。因此程序读取INFO.txt,其中包含所有电子邮件身份验证g_userg_passm_num。然后它使用这些发送一封发送短信的电子邮件。

现在我的问题是UnboundLocalError: local variable 'g_user' referenced before assignment。我知道这是由不是global 变量引起的。谁能帮我吗?我被难住了……

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()

【问题讨论】:

    标签: python python-3.x tkinter global


    【解决方案1】:

    问题很可能出在这一行:

    sender = '{}@gmail.com'.format(g_user)
    

    因为 if 语句条件 (if 'GMail_name:::' in line) 的计算结果为 False,然后您的 g_user 变量永远不会在该函数的本地范围内定义。

    【讨论】:

      【解决方案2】:

      仔细查看错误信息:

      UnboundLocalError: local variable 'g_user' referenced before assignment
      

      一个很好的经验法则是假设错误消息说的是真的。在这种情况下,它告诉您两个非常重要的细节:

      • 它认为g_user 是一个局部变量
      • 它认为g_user在设置之前就被使用了

      要解决此问题,您需要对其中一个或两个问题回答为什么为什么它认为它是本地的,和/或为什么它认为它没有设置?如果您在脑海中单步执行代码,您可能会回答其中一个或两个问题。

      例如,问自己一个问题“如果'GMail_name:::' in line 返回 false,g_user 是如何设置的?您是否验证过 if 语句是正确的?您的代码是否准备好处理错误的情况?您是否确实向自己证明了 if 语句是正确的,或者您只是假设它是正确的?

      另外,请回答这个问题:您是阅读 INFO.txt 中的每一行,还是阅读单行?如果您只阅读一行,那是故意的吗?看起来您希望用户名和密码都位于行中的位置 [13:],如果两个值不同并且两个值在同一行上,这是不可能的。

      由于您刚刚学习编程,因此不要只是将代码行扔到文件中并希望它们能正常工作,也不要让其他人解决您的问题。 想想电脑在做什么。逻辑地单步执行代码,问题将不言而喻。

      【讨论】:

        猜你喜欢
        • 2013-11-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-14
        相关资源
        最近更新 更多