我有一个程序,它将从GUI窗口中获取文本和信息,并发送自动书面电子邮件。这是一个校园工作(实验室服务员)我已经厌倦了为在计算机实验室留下东西的学生一遍又一遍地写同样的电子邮件。我第一次用Python编写(来自Java和C)。
我打算获取信息,让对象Lab Attendant和Student获取包含电子邮件,密码,名称等的所有信息。然后,它会根据对象中的信息创建一个字符串,并使用smtplib发送电子邮件。
我遇到的问题是,当它在对象中设置属性时,从第一个实验室助理属性电子邮件开始,它只是在set函数中反复循环。
我不确定为什么,我已经尝试过给它一个突破但是它甚至没有达到那条线。它只是停留在第52行
self.email = email
这是完整的代码。
from tkinter import * # GUI library
import tkinter.messagebox
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# @author: xxxx
# Sends automated emails out for lab attendant job
class Student:
def __init__(self, name, email, item):
self.name = name
self.email = email
self.item = item
@property
def email(self):
return self.email
@email.setter
def email(self, email):
self.email = email
@property
def name(self):
return self.name
@name.setter
def name(self, name):
self.name = name
@property
def item(self):
return self.item
@item.setter
def item(self, item):
self.item = item
class LabAttendant:
def __init__(self, email, password, lab):
self.email = email
self.password = password
self.lab = lab
@property
def email(self):
return self.email
@email.setter
def email(self, email):
self.email = email
@property
def password(self):
return self.password
@password.setter
def password(self, password):
self.password = password
@property
def lab(self):
return self.lab
@lab.setter
def lab(self, password):
self.password = password
def SendEmail(labattendant, student):
# message = MIMEMultipart()
# message['From'] = labattendant.email
# message['To'] = student.email
# message['Subject'] = student.item + " left in " + labattendant.lab
if "id" == student.item:
body = "Hello " + student.name + ",\n I believe you left your " + student.item + " in the " + labattendant.lab \
+ " computer lab. If so, please stop by the " + labattendant.lab + " computer lab "
else:
body = "Hello " + student.name + ",\n I believe you left your " + student.item + " in the " + labattendant.lab\
+ " computer lab. If so, please stop by the " + labattendant.lab\
+ " computer lab with your ID and ask the lab " + "attendant to return your" + student.item + "to you.\n"
class GUI:
def __init__(self, master):
frame = Frame(master)
frame.pack()
student_info_frame = Frame(frame)
student_info_frame.pack(side=TOP)
labattendant_info_frame = Frame(frame)
labattendant_info_frame.pack(side=TOP)
# Student name, email, item
student_name_label = Label(student_info_frame, text="Student name: ")
student_name_label.grid(row=0, column=0)
student_name_entry = Entry(student_info_frame)
student_name_entry.grid(row=0, column=1)
student_email_label = Label(student_info_frame, text="Student email: ")
student_email_label.grid(row=1, column=0)
student_email_entry = Entry(student_info_frame)
student_email_entry.insert(END, '@school.edu')
student_email_entry.grid(row=1, column=1)
student_item_label = Label(student_info_frame, text="Item left behind: ")
student_item_label.grid(row=2, column=0)
student_item_entry = Entry(student_info_frame)
student_item_entry.grid(row=2, column=1)
# Lab attendant email, password, lab working in
labattendant_email_label = Label(labattendant_info_frame, text="Your email: ")
labattendant_email_label.grid(row=3, column=0)
labattendant_email_entry = Entry(labattendant_info_frame)
labattendant_email_entry.insert(END, '@school.edu')
labattendant_email_entry.grid(row=3, column=1)
labattendant_password_label = Label(labattendant_info_frame, text="Email password: ")
labattendant_password_label.grid(row=4, column=0)
labattendant_password_entry = Entry(labattendant_info_frame, show='*')
labattendant_password_entry.grid(row=4, column=1)
labattendant_lab_label = Label(labattendant_info_frame, text="Current lab: ")
labattendant_lab_label.grid(row=5, column=0)
lab_defualt = StringVar(labattendant_info_frame)
labattendant_lab_menu = OptionMenu(labattendant_info_frame, lab_defualt, "MCT054", "CUB124", "GRH106")
labattendant_lab_menu.grid(row=5, column=1)
# Button handler for the submit button
def submit_on_click():
if "@school.edu" not in labattendant_email_entry.get():
tkinter.messagebox.showwarning("Error", "Please use your @school.edu email.")
labattendant_email_entry.delete(0, 'end')
else:
labattendant = LabAttendant(labattendant_email_entry.get(), labattendant_password_entry.get(),
labattendant_lab_menu.cget("text"))
student = Student(student_name_entry.get(), student_email_entry.get(), student_item_entry.get().lower())
SendEmail(labattendant, student)
# Collects data and sends it
submit_button = Button(frame, text="Send Email", command=submit_on_click)
submit_button.pack(side=BOTTOM)
root = Tk()
GUI = GUI(root)
root.mainloop()
答案 0 :(得分:4)
在Python中,您不需要getter和setter来进行普通的属性访问。仅当用户访问和/或修改属性时需要运行特殊代码时,才使用@property
。当你这样做时,你必须注意命名,以避免遇到问题。
class Student(object):
def __init__(self, name, email, item):
# For example, there might be no need for name and item properties.
self.name = name
self.item = item
# But maybe we want to validate email addresses.
# Note that we will store the email address in self._email.
self._email = None
self.email = email
@property
def email(self):
return self._email
@email.setter
def email(self, email):
if email_is_valid(email):
self._email = email
else:
raise ValueError('Invalid email')
def email_is_valid(email):
return '@' in email
print Student('x', 'a@foo.com', 1).email
print Student('y', 'bar', 2).email
答案 1 :(得分:2)
属性名称不能与属性名称相同(在同一个类中)。