我目前正在尝试创建一个基本的 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()