首页 > 解决方案 > 关于文本输入框,在 Tkinter 上使用 .get() 函数时遇到问题

问题描述

我正在尝试制作自己的“电子邮件界面”,我需要在其中通过输入框收集用户的输入,以便在电子邮件发送过程中使用它们。问题是 .get() 功能似乎总是出错。

line 32, in send_it email_resipient = Three.get()
AttributeError: 'NoneType' object has no attribute 'get'

请帮助,我已经研究了几个小时,但我似乎无法找到解决方法。这是代码....

from tkinter import *
import smtplib


root = Tk()
root.title("Jon's Email Service")
root.geometry("800x640+0+0")

Label(root, text="Jon's Email Service", font=("arial", 60, 
"bold"), fg="black").pack()

Label(root, text="User's Email address {Has to be gmail} ", 
font=("arial", 20,), fg="black").pack()

One = Entry(root,width=40, bg="white").pack()


Label(root, text="User's Gmail Password", font=("arial", 20,), 
fg="black").pack()

Two = Entry(root, width=40, bg="white").pack()



Label(root, text="Email Recipient", font=("arial", 20,), 
fg="black").pack()

Three = Entry(root,width=40, bg="white").pack()
Label(root, text="The Message", font=("arial", 20,), 
fg="black").pack()

Four = Entry(root, width=60, bg="white").pack()

def send_it():
    email_resipient = Three.get()
    emailUser = One.get()
    user_Password = Two.get
    msg = Four.get()
    print(emailUser)
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(emailUser, user_Password)
    server.sendmail(emailUser, email_resipient, msg)
    server.quit()
Label(root, text="Email Is Sent!", font=("arial", 20,), 
fg="black").pack()

send = Button(root, text="Send", width = 40, bg = "lightblue", 
command = send_it).pack()

root.mainloop()

标签: pythontkinter

解决方案


您不应该.pack()在分配字符串的末尾使用,您应该首先分配变量,然后.pack()是:

from tkinter import *
import smtplib


root = Tk()
root.title("Jon's Email Service")
root.geometry("800x640+0+0")

Label(root, text="Jon's Email Service", font=("arial", 60, 
"bold"), fg="black").pack()

Label(root, text="User's Email address {Has to be gmail} ", 
font=("arial", 20,), fg="black").pack()

One = Entry(root,width=40, bg="white")
One.pack() #here

Label(root, text="User's Gmail Password", font=("arial", 20,), 
fg="black").pack()

Two = Entry(root, width=40, bg="white")
Two.pack() # here 

Label(root, text="Email Recipient", font=("arial", 20,), 
fg="black").pack()

Three = Entry(root,width=40, bg="white")
Three.pack() # here
Label(root, text="The Message", font=("arial", 20,), 
fg="black").pack()

Four = Entry(root, width=60, bg="white")
Four.pack() # here

def send_it():
    email_resipient = Three.get()
    emailUser = One.get()
    user_Password = Two.get
    msg = Four.get()
    print(emailUser)
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(emailUser, user_Password)
    server.sendmail(emailUser, email_resipient, msg)
    server.quit()
Label(root, text="Email Is Sent!", font=("arial", 20,), 
fg="black").pack()
send = Button(root, text="Send", width = 40, bg = "lightblue", 
command = send_it).pack()

root.mainloop()

变量获取pack()函数的返回值,而不是实际的 aEntry或其他值,而返回的pack()None


推荐阅读