Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在调用Python变量之前调用它们';重新定义_Python_Tkinter_Imaplib - Fatal编程技术网

在调用Python变量之前调用它们';重新定义

在调用Python变量之前调用它们';重新定义,python,tkinter,imaplib,Python,Tkinter,Imaplib,我正在使用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)')

我正在使用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)

基本上,我的问题是,我想将我在第一个代码片段中解析的电子邮件数据加载到我在第二个代码窗口中创建的窗口中。由于Python的格式,在调用之前必须放置def-lb_-leftclick_处理程序,但是,我无法将数据加载到窗口中,因为它还不存在。有解决办法吗?很抱歉,如果这个问题的措辞很糟糕。

Python正在解释代码。如果未调用,它将不会执行该函数。您可以安全地将函数代码放在上面,以便在您调用它时对其进行定义,并且在您调用它时,它将具有所需的所有数据。

只需使用类即可。它们甜美且功能强大,在类中顺序等并不重要。如果您的代码段出现在函数中,只需确保在调用该函数之前定义了
lb_leftclick_handler
。如果它不在函数中,那么它应该在函数中。谢谢你的回答,我甚至没有考虑过它可以放在列表框的前面,有没有办法记录列表框中选择了哪些项目,以便加载正确的主题/正文?我从来没有使用过Tkinter。在文档中,一定有一些东西可以看到哪个被选中了。
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')