在定义变量之前调用 Python 个变量
Calling Python variables before they're defined
我正在使用 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')
基本上我的问题是我想将我在第一个代码片段中解析的电子邮件数据加载到我在第二个 window 代码中创建的 window 中。由于 Python 的格式,def lb_leftclick_handler 必须在调用之前放置,但是,我无法将数据加载到 window 中,因为它尚不存在。有解决方法吗?抱歉,如果问题措辞糟糕。
Python 正在解释代码。如果不调用,它将不会执行该函数。你可以安全地将函数代码放在上面,这样当你调用它时它就会被定义,当你调用它时,它就会拥有它需要的所有数据。
我正在使用 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')
基本上我的问题是我想将我在第一个代码片段中解析的电子邮件数据加载到我在第二个 window 代码中创建的 window 中。由于 Python 的格式,def lb_leftclick_handler 必须在调用之前放置,但是,我无法将数据加载到 window 中,因为它尚不存在。有解决方法吗?抱歉,如果问题措辞糟糕。
Python 正在解释代码。如果不调用,它将不会执行该函数。你可以安全地将函数代码放在上面,这样当你调用它时它就会被定义,当你调用它时,它就会拥有它需要的所有数据。