我目前正在开发一个python tkinter应用程序,它允许用户使用GUI窗口向用户发送电子邮件。我在python 3.4.2上运行,并创建了[登录]窗口和[撰写消息]窗口。在我的程序中,我创建了许多用户输入数据的条目,一个用于电子邮件,密码,消息,主题,到等等。然后我有一个函数print()输出用户输入的所有数据。唯一的问题是控制台中只显示3/6条目结果。有人可以看看我的代码并告诉我有什么问题。我的朋友请不要标记我的问题,我只是想了解为什么这不起作用,所以我可以在这上面工作并在网上发布,如果你想我可以在工作中把你归功于#&# 39; s你想要的;)下面我提供了控制台输出的屏幕截图,以及我创建的2个不同的窗口。
代码:
#-->Project name : Email Buddy
#-->Project start date : Monday, August 24th, 2015.
#-->Project written by : Pamal Mangat.
#-->Project end date :
#Imports
from PIL import Image, ImageTk
import webbrowser
import email
import sys
import smtplib
import tkinter
def sendMail(recipient, sender, subject, message, password):
#Function sends the email.
msg = email.message_from_string(message)
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
connect = smtplib.SMTP("smtp.live.com",587)
connect.ehlo()
connect.starttls()
connect.ehlo()
connect.login(password, password)
connect.sendmail(sender, recipient, msg.as_string())
connect.quit()
def root():
def testInfo(email, password, sender, reciever, message, subject):
#Function checks if all info is entered.
print("Email : " + email)
print("Password : " + password)
print("From : " + sender)
print("To : " + reciever)
print("Message : " + message)
print("Subject : " + subject)
#Created a send message (compose message) window (root = Compose Message) >>> User is redirected to this page after signing in.
root = tkinter.Tk()
root.title("Email Buddy | Compose Message")
#Root text
root_text1 = tkinter.Label(root, text="To", font="Bizon 20 bold", fg="Red")
root_text1.place(x=20, y=50)
root_text2 = tkinter.Label(root, text="Subject", font="Bizon 20 bold", fg="Red")
root_text2.place(x=20, y=145)
root_text3 = tkinter.Label(root, text="Message", font="Bizon 20 bold", fg="Red")
root_text3.place(x=20, y=235)
#Root entries
to_Variable = tkinter.StringVar()
to_Entry = tkinter.Entry(root, bd=4, width=28, textvariable=to_Variable, font="Helvetica 16 italic")
to_Entry.focus()
to_Entry.place(x=20, y=95)
subject_Variable = tkinter.StringVar()
subject_Entry = tkinter.Entry(root, bd=4, width=28, textvariable=subject_Variable, font="Helvetica 16 italic")
subject_Entry.focus()
subject_Entry.place(x=20, y=190)
message_Variable = tkinter.StringVar()
message_Entry = tkinter.Entry(root, textvariable=message_Variable, bd=4, width=28, font="Helvetica 16 italic")
message_Entry.focus()
message_Entry.place(x=20, y=280)
email = email_Variable.get()
password = password_Variable.get()
sender = email_Variable.get()
reciever = to_Variable.get()
message = message_Variable.get()
subject = subject_Variable.get()
send_Button = tkinter.Button(root, text="Send", fg="White", bg="Green", width=8, height=2, font="Bizon 8 bold", command=lambda:testInfo(email, password, sender, reciever, message, subject))
send_Button.place(x=300, y=355)
#Locks window size.
root.minsize(385,425)
root.maxsize(385,425)
#Changes favicon in upper-left corner of master window.
root.iconbitmap(r"C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Email Buddy\Images\EmailBuddy_Icon.ico")
root.mainloop()
#Created a main log in window (master = Log In).
master = tkinter.Tk()
master.title("Email Buddy | Log In")
#Master text
master_text = tkinter.Label(master, text="Sign In", font="Bizon 36 bold", fg="Red")
master_text.place(x=90, y=20)
master_text2 = tkinter.Label(master, text="Email", font="Bizon 20 bold")
master_text2.place(x=20, y=100)
master_text3 = tkinter.Label(master, text="Password", font="Bizon 20 bold")
master_text3.place(x=20, y=190)
#Master entries
email_Variable = tkinter.StringVar()
email_Entry = tkinter.Entry(master, bd=4, width=25, textvariable=email_Variable, font="Helvetica 16 italic", fg="Red")
email_Entry.focus()
email_Entry.place(x=20, y=140)
password_Variable = tkinter.StringVar()
password_Entry = tkinter.Entry(master, bd=4, width=25, textvariable=password_Variable, font="Helvetica 16 italic", fg="Red")
password_Entry.focus()
password_Entry.place(x=20, y=230)
#Sign-In Button
signIn_Button = tkinter.Button(master, text="Sign In", font="Bizon 8 bold", fg="White", bg="Green", width=8, height=2, command=lambda:root())
signIn_Button.place(x=265, y=295)
#Locks window size
master.minsize(350, 350)
master.maxsize(350, 350)
#Changes favicon in upper-left corner of master window.
master.iconbitmap(r"C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Email Buddy\Images\EmailBuddy_Icon.ico")
master.mainloop()
截图:
第一个窗口
第二个窗口
答案 0 :(得分:1)
问题的关键在于,当您第一次创建按钮时,您将获得变量的值,此时,to,subject和message为空白。
编写GUI的正确方法是在需要时获取值,而不是之前。这消除了lambda
的需要; lambda很少是真正必要的,它使代码更难编写,更难理解,更难修改。
另外,我的建议是不使用StringVars,并修改testInfo
以在需要时获取数据。使用StringVar
只是您需要跟踪的另一个对象,除非您正在使用这些对象的特殊功能(您不是这样),否则完全没有必要。
def testInfo():
reciever = to_Entry.get()
email = email_Entry.get()
password = password_Entry.get()
sender = email_Entry.get()
message = message_Entry.get()
subject = subject_Entry.get()
print("Email : " + email)
print("Password : " + password)
print("From : " + sender)
print("To : " + reciever)
print("Message : " + message)
print("Subject : " + subject)
...
send_Button = tkinter.Button(..., command=testInfo)
答案 1 :(得分:1)
只是一个额外的提示: 而不是定义根的最小和最大大小。你可以简单地做:
root.resizable(False, False)
第一个假代表x轴而另一个假代表y ..