0

我目前正在尝试创建一个基本的 Gmail 客户端,并且我正在尝试在两个类之间共享我的登录详细信息,Authen并且Application. 我需要访问在Authen课堂上输入的学分,但我不知道该怎么做。这是我当前的代码,在课堂上Application

Username_mine = passcheck.Create_Widgets.Username.get()
Password_mine = passcheck.Create_Widgets.Password.get()
contents = self.Body.get("0.0", END)
FROM = "Unknown"
subject = self.Subject_Entry.get()
recipients = self.To_Entry.get()
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(Username_mine, Password_mine)
server.sendmail(FROM, recipients, contents)

当我运行代码时,我收到错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "/Users/BigKids/Desktop/Coding/Python 2/Email/Email Send GUI V1.py", line 112, in send_cmd
    Username_mine = passcheck.Create_Widgets.Username.get()
AttributeError: 'function' object has no attribute 'Username'

预先感谢您的帮助。

编辑这是我的代码:

#Email Send v3 Program. It has a basic GUI to send emails.

from Tkinter import *
import smtplib
import string
import random

def random_char():
    char_select = random.randrange(52)
    char_choice = string.ascii_letters[char_select]
    return char_choice

class Authen(Frame):
    """Holds authentication code and basic GUI stuff"""
    def __init__(self, master):
        """Start Authen GUI"""
        Frame.__init__(self, master)
        self.grid()
        self.Create_Widgets()

    def Create_Widgets(self):
        """Spawns the widgets"""
        self.Usertext = Label(self, text = "Username: ")
        self.Usertext.grid(row = 0, column = 0)
        self.Username = Entry(self)
        self.Username.grid(row = 0, column = 1)

        self.Passtext = Label (self, text = "Password: ")
        self.Passtext.grid(row = 1, column = 0)
        self.Password = Entry (self, show = "*")
        self.Password.grid(row = 1, column = 1)

        self.Submit = Button(self, text = "Submit Credits",
                        command = self.authen_credits)
        self.Submit.grid()

    def authen_credits(self):
        """Backbone of authen process"""
        Username_mine = self.Username.get()
        Password_mine = self.Password.get()
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        while True:
            try:
                server.login(Username_mine,Password_mine)
                #make my screen dimensions work
                w = 500
                h = 1000
                app = Application()
                app.title("SMTP Mail Client")
                app.geometry("%dx%d" % (w, h))
                break
            except smtplib.SMTPAuthenticationError:
                print "Login Failed"

class Application(Toplevel):
    """Toplevel Frame, holds sendmail stuff"""
    def __init__(self):
        """Start the toplevel Frame"""
        Toplevel.__init__(self)
        self.grid()
        self.entryFun()
        self.mainFun()

    def entryFun(self):
        """Holds entry Frame"""
        self.entryFrame = Frame(self)
        self.entryFrame.grid(row = 0, column = 0, sticky = W)

        self.To_Lbl = Label(self.entryFrame, text = "TO:  ")
        self.To_Lbl.grid(row = 0, column = 0, sticky = W)

        self.To_Entry = Entry(self.entryFrame)
        self.To_Entry.grid(row = 0, column = 1, sticky = W)


        self.Subject_Lbl = Label(self.entryFrame, text = "Subject:  ")
        self.Subject_Lbl.grid(row = 1, column = 0, sticky = W)

        self.Subject_Entry = Entry(self.entryFrame)
        self.Subject_Entry.grid(row = 1, column = 1, sticky = W)

    def mainFun(self):
        """Holds body of thing"""
        self.entryFrame = Frame(self)
        self.entryFrame.grid(row = 1, column = 0, sticky = W)


        self.Body = Text(self.entryFrame, width = 75, height = 50, relief = GROOVE, bd = 5)
        self.Body.grid()

        self.send_mail = Button(self, text = "Send Mail",
                                command = self.send_cmd)
        self.send_mail.grid()

    def send_cmd(self):
        Username_mine = passcheck.Create_Widgets.Username.get()
        Password_mine = passcheck.Create_Widgets.Password.get()
        contents = self.Body.get("0.0", END)
        FROM = "Unknown"
        subject = self.Subject_Entry.get()
        recipients = self.To_Entry.get()
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login()
        server.sendmail(FROM, recipients, contents)

root = Tk()
root.title("Login")
root.geometry("500x500")
passcheck = Authen(root)

root.mainloop()
4

3 回答 3

1

您收到错误是因为它Create_Widgets()是类的方法函数Authen,所以self.Username = Entry(self)它是Username实例的属性,而不是方法。鉴于此,假设passcheck是这样一个实例,我认为您需要编写:

Username_mine = passcheck.Username.get()
于 2013-06-14T01:21:24.537 回答
0

Create_Widgets 是一个函数而不是一个类,因此您无法访问其中的 .Username 。

您需要做的是将 Authen 的实例传递给 Application 类,并将其存储在 self.authen 中。然后Application中的函数可以做:self.authen.Username

通过简要浏览您的代码,也许您可​​以修改您的__init__for Application,使其接受“authen”变量:

def __init__(self, authen):
  # Other stuff.
  self.authen=authen 

然后你会初始化应用程序 app = Application(authen=self)

在 中send_cmd(),您会调用:Username_mine = self.authen.Username.get()

于 2013-06-14T01:21:15.647 回答
0

您确定 Username 是函数对象的实际成员吗?(确保您在函数类中使用self.Username而不是Username )

于 2013-06-14T00:21:48.207 回答