带有 tkinter 的 Python 属性设置器无限循环

Car*_*son 1 python tkinter

我有一个程序可以从 GUI 窗口接收文本和信息,并自动发送一封书面电子邮件。这是一份校园工作(实验室服务员),我厌倦了一遍又一遍地为那些把东西留在计算机实验室的学生写同样的电子邮件。我几乎是第一次用 Python 编写(来自 Java 和 C)。

我打算接收信息,为包含电子邮件、密码、姓名等的所有信息创建对象实验室服务员和学生。然后它从对象中的信息创建一个字符串,并使用 smtplib 发送电子邮件。

我遇到的问题是,当它在对象中设置属性时,从第一个实验室服务员属性电子邮件开始,它只会在 set 函数中一遍又一遍地循环。

在此处输入图片说明

我不知道为什么,我试过让它回归以突破,但它甚至没有到达那条线。它只是停留在第 52 行

   self.email = email
Run Code Online (Sandbox Code Playgroud)

这是完整的代码。

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()
Run Code Online (Sandbox Code Playgroud)

FMc*_*FMc 5

在 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
Run Code Online (Sandbox Code Playgroud)