我正在使用Python创建一个电子邮件客户端,并且在代码格式方面遇到了一些小问题。我正在使用的库是imaplib和tkinter(gui)
我有这段代码在列表框中显示电子邮件:
for i in range(latest_eid, latest_eid-15, -1):
i = str (i) #This block of code loads the 15 most recent emails
typ, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1].decode('UTF-8'))#Converts the content of the email data to string
eSubject = msg['subject']#Variable for the subject of each email
eFrom = msg['from']#Variable for the sender of each email
eFrom = eFrom.replace('<','')
eFrom = eFrom.replace('>','')#Deleting the < & > from the senders email
if len(eSubject) > 30:
eSubject = eSubject[0:28] + '...' #Load only the first 30 characters of the subject
lb = Listbox(rootA) #Listbox to show the emails in the gui
lb.pack()
lb.insert(END, 'From: (' + eFrom.split()[-1] + ') Subject:' + eSubject)
lb.configure(background='black', fg='white', height=2, width=85, font=('Arial', 10))
lb.bind('<Double-Button-1>', lb_leftclick_handler)
所以现在我想定义我在这里完成的lb_leftclick_handler:
def lb_leftclick_handler(msg):
rootB = Tk()
rootB.title('Email')
rootB.configure(background='black')
bodyL = Label(rootB, text='(RFC822)')
bodyL.configure(background='black', fg='white')
基本上我的问题是我想将我在第一段代码中解析的电子邮件数据加载到我在第二个代码窗口中创建的窗口中。由于Python的格式如何,def lb_leftclick_handler必须在调用之前放置,但是,我无法将数据加载到窗口中,因为它还不存在。这有解决方法吗?如果问题措辞严厉,那就很抱歉。
答案 0 :(得分:-1)
Python正在解释代码。如果不调用它将不会执行该功能。你可以放心地输入上面的功能代码,以便在你调用它时定义它,当你调用它时,它将拥有它需要的所有数据。