python - 关于文本输入框,在 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()
解决方案
您不应该.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
。
推荐阅读
- java - Java Open CV 3.4.2 如何从 Mat Object 中提取不同的通道?
- javascript - 无法自动从 ng-class 添加/删除类以切换选定的选项卡
- vim - Vim 从可视模式中选择的文本中搜索和替换(全部)
- php - 在 laravel 刀片中访问多维数组
- c# - 正则表达式模式仅在第二个交替时匹配
- angular - 如何使用插值更改角度 6 中的变量?
- node.js - 节点 fs.readFile 函数
- sql - 跨多个表删除多行
- c# - 库存类别系统的 TreeView 中的 C# 唯一 ID
- openstreetmap - 与 OpenStreetMap 相比,使用边界框的 HERE Map Image API 得到不同的结果