Problem with tkinter entrybox. AttributeError: 'StringVar' object has no attribute 'encode'

Problem with tkinter entrybox. AttributeError: 'StringVar' object has no attribute 'encode'

我试图用 Tkinter 和 smtplib 创建一个电子邮件发送程序,但我遇到了 StringVar() 函数的问题。在与 Entrybox 一起使用之前,我用 stringvar 定义了所有变量,但无论我尝试什么,我都会得到同样的错误。我看到了 ,但我无法将答案改编成我自己的代码。

我得到的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:/Users/User/PycharmProjects/alistirmalar/Email Sender.py", line 39, in <lambda>
    Button(root,text ='\u2386',command=lambda:[send_mail()], font = 'arial 15 bold',height = 1,width = 6,bg = 'ghost white', padx = 2).place(x=455 ,y =404)
  File "C:/Users/User/PycharmProjects/alistirmalar/Email Sender.py", line 19, in send_mail
    mail.login(from_,passphrase)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 721, in login
    (code, resp) = self.auth(
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 632, in auth
    response = encode_base64(initial_response.encode('ascii'), eol='')
AttributeError: 'StringVar' object has no attribute 'encode'

我的全部代码:

import smtplib
from tkinter import *

root = Tk()
root.geometry('600x450')
root.resizable(0,0)
root.title("Gmail Post Service - Pre-alpha")

def send_mail():
    global title,text,from_,passphrase,to,message

    mail = smtplib.SMTP('smtp.gmail.com',587)
    mail.ehlo()

    message = 'Subject: {}\n\n{}'.format(title,'xd')

    mail.starttls()
    mail.login(from_,passphrase)
    mail.sendmail(from_,to,message)

    mail.close()


from_= StringVar()
to= StringVar()
passphrase= StringVar()
title= StringVar()
text = StringVar()

Label(root, text = 'From:', font = 'arial 12 bold').place(x= 5 , y = 20)
from2=Entry(root, width=30,textvariable=from_).place(x=55,y=25)

Label(root, text = 'Password:', font = 'arial 12 bold').place(x= 250 , y = 20)
passphrase2=Entry(root, width=30,textvariable=passphrase).place(x=340,y=25)

Label(root, text = 'To:', font = 'arial 12 bold').place(x= 20 , y = 50)
to2=Entry(root, width=30,textvariable=to).place(x=55,y=55)

Label(root, text = 'Title:', font = 'arial 12 bold').place(x= 5 , y = 95)
title2=Entry(root, width=80,textvariable=title).place(x=55,y=100)

t = Text(root, height=16, width=60).place(x=55,y=140)

Button(root,text ='\u2386',command=lambda:[send_mail()], font = 'arial 15 bold',height = 1,width = 6,bg = 'ghost white', padx = 2).place(x=455 ,y =404)

root.mainloop()

提前感谢您的回复!

passphrasetofrom_ 都是 StringVar 所以你需要调用 .get() 来获取值。

mail.login(from_.get(),passphrase.get())
mail.sendmail(from_.get(),to.get(),message)