如何在Python中共享类之间的变量

时间:2013-06-13 23:21:13

标签: python python-2.7 smtp

我目前正在尝试创建一个基本的Gmail客户端,我正在尝试在两个类AuthenApplication之间共享我的登录详细信息。我需要访问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()

3 个答案:

答案 0 :(得分:1)

您收到错误是因为Create_Widgets()Authen类的方法函数,因此其中的self.Username = Entry(self)使Username成为实例的属性,不是方法。鉴于此,假设passcheck是这样的实例,我认为你需要写:

Username_mine = passcheck.Username.get()

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

从简要略过代码开始,也许您可​​以修改应用程序的__init__,以便它接受“authen”变量:

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

然后,您将初始化应用程序 app = Application(authen=self)

send_cmd()中,您可以致电:Username_mine = self.authen.Username.get()