gpt4 book ai didi

python - 如何使用 python 检索条目和文本小部件的内容并将其分配给 tkinter 中的变量

转载 作者:行者123 更新时间:2023-11-28 18:47:35 27 4
gpt4 key购买 nike

好吧,我有以下代码,是我为使用 python 创建基于 gui 的电子邮件客户端而编写的。我使用了 tkinter。我希望用户在条目小部件中写入的文本被分配给变量发件人、密码、接收者、消息.我已经使用条目小部件来获取消息的发件人、密码、接收者和文本小部件的值。怎么做?我为此尝试了很多方法(如 get 方法、textvariable 方法)但它没有被执行。我我是新手,因此更喜欢不涉及类(class)的答案。请尽快回复!这很紧急。感谢您的帮助。我只是向您展示我在为变量 sender 赋值时所做的事情,但我希望所有四个变量。

import smtplib
from Tkinter import *
import tkMessageBox


def Composemail(sender,password,receivers,message):
try:
server = smtplib.SMTP()
server.connect('smtp.gmail.com',587) # for eg. host = 'smtp.gmail.com', port = 587
server.ehlo()
server.starttls()
server.login(sender, password)
server.sendmail(sender, receivers, message)
#smtpObj = smtplib.SMTP_SSL('smtp.gmail.com',587)
#smtpObj.sendmail(sender, receivers, message)
tkMessageBox.showinfo("Sending Mail information","Mail sent.")
except smtplib.SMTPException, error:
tkMessageBox.showinfo("Sending Mail information","Sending Mail failed.Try again later.")

a=Tk()
a.title("MailsNow-A new place for sending emails")
a.geometry("1000x700")
b=Label(a,fg="Purple",text="From")
b.pack()
sender=StringVar() #the problem starts here
c=Entry(a,bd=5,width=100,textvariable=sender)
c.pack()
d=Label(a,fg="Purple",text="Password")
d.pack()
e=Entry(a,bd=5,width=100,show="*")
e.pack()
password='abc'
f=Label(a,fg="Purple",text="To")
f.pack()
receivers = 'abc@gmail.com'
g=Entry(a,bd=5,width=100)
g.pack()
h=Label(a,fg="Purple",text="Subject")
h.pack()
i=Entry(a,bd=5,width=100)
i.pack()
j=Label(a,fg="Purple",text="Type your email here")
j.pack()
k=Text(a,bd=5,height=20,width=100)
k.pack()
message = """From: From Person <abc@gmail.com>#I want to know how to do the same for text #widget too
To: To Person <xyz@gmail.com>
Subject: SMTP e-mail test

Hi , now I can send you emails using this......hurrah1!using Sending mail transfer protocol by a python
code!!!!
"""
l=Button(a,
text="Sendmail",bg="Purple",activebackground="Yellow",
command=lambda:Composemail(sender,password,receivers,message))
l.pack()
a.mainloop()

最佳答案

在您的特定情况下,您不需要使用 StringVar。这应该是你想要的:

import smtplib
from Tkinter import *
import tkMessageBox

def Composemail(sender,password,receivers,message):
try:
server = smtplib.SMTP()
server.connect('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login(sender, password)
server.sendmail(sender, receivers, message)
tkMessageBox.showinfo("Sending Mail information","Mail sent.")
# Just a tip, "error" isn't defined yet so it will blow up.
except smtplib.SMTPException, error:
tkMessageBox.showinfo("Sending Mail information","Sending Mail failed.Try again later.")

a=Tk()
a.title("MailsNow-A new place for sending emails")
a.geometry("1000x700")
b=Label(a,fg="Purple",text="From")
b.pack()
c=Entry(a,bd=5,width=100)
c.pack()
d=Label(a,fg="Purple",text="Password")
d.pack()
e=Entry(a,bd=5,width=100,show="*")
e.pack()
f=Label(a,fg="Purple",text="To")
f.pack()
g=Entry(a,bd=5,width=100)
g.pack()
h=Label(a,fg="Purple",text="Subject")
h.pack()
i=Entry(a,bd=5,width=100)
i.pack()
j=Label(a,fg="Purple",text="Type your email here")
j.pack()
k=Text(a,bd=5,height=20,width=100)
k.pack()

def getstuff():
# You can retrieve the text entered into an entrybox using the get method
sender=c.get()
password=e.get()
receivers=g.get()
subject=i.get()
# Since textboxes are multiline, I have to tell it what text to get.
# 0.0,END is saying "get everything from start to finish".
message=k.get(0.0,END)

# One thing I noted was that you are not sending the subject to
# Composemail. Is this what you want?
Composemail(sender,password,receivers,message)

l=Button(a, text="Sendmail",bg="Purple",activebackground="Yellow",
command=getstuff)
l.pack()

a.mainloop()

当您单击“发送邮件”按钮时,您输入到应用程序中的所有内容都会被收集并发送到 Composemail。从那里,你可以用它做任何你想做的事。

附注我舍弃了您的一些代码,以便更好地解决您的问题。

关于python - 如何使用 python 检索条目和文本小部件的内容并将其分配给 tkinter 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17673421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com