F
F
Fizl2021-06-13 22:46:19
Python
Fizl, 2021-06-13 22:46:19

How to interact with a function variable in another function in the same class?

I hope that I asked the question correctly)
There is a variable "self.text_show", it is located in the MAIN_WIND class of the init_main function. I try to interact with this variable in the follow_2 function, but for some reason it still throws an error, although I can do the same with the text_enteremail variable and the follow function. Help me fix this (only part of the code is here, so there are no classes that interact with)

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import codecs
import email
import imaplib
from threading import Thread
import threading
from tkinter import messagebox
from email.header import decode_header
import codecs
import time
import os

class MAIN_WIND(tk.Frame):
    def __init__(self, main_root):
        super().__init__(main_root)
        self.contact_name = "TXT"
        self.MAIL = MAIL('ПОЧТА', smtplib.SMTP('smtp.gmail.com:587'))
        self.GET_MAIL = GET_MAIL(imaplib.IMAP4_SSL('imap.gmail.com'))
        threading.Thread(target = self.GET_MAIL.get_mail_func, daemon=True).start()
        threading.Thread(target = self.follow_2, daemon = True).start()
        self.init_main()

    def follow(self, name):
        current = codecs.open(name, "r", encoding = "utf-8")
        curino = os.fstat(current.fileno()).st_ino
        while True:
            while True:
                line = current.readline()
                if not line:
                    break
                yield line

            try:
                if os.stat(name).st_ino != curino:
                    new = open(name, "r")
                    current.close()
                    current = new
                    curino = os.fstat(current.fileno()).st_ino
                    continue
            except IOError:
                pass
            time.sleep(1)

    def follow_2(self):
        if __name__ == '__main__':
            for l in self.follow(f"{self.contact_name}.txt"):
                #print(format(l))
                self.text_show.insert(1.0, l)


    def retrieve_mail(self):
        self.retr_mail = self.text_enteremail.get(1.0, tk.END)
        self.MAIL.send_mail(self.retr_mail)

    def init_main(self):
        self.MAIL.start_client('smtp.gmail.com:587')

        self.btn_dialog_sendmail = tk.Button(main_root, text = '➤', padx = 8, pady = 24, font = ('Ubuntu',25), command = self.retrieve_mail)
        self.text_enteremail = tk.Text(main_root, width = 30, height = 6, bg='white', fg='black', font = ('COMIC SANS MS',10))

        #////SCROLLBAR////
        self.dialog_frame = tk.Frame(main_root)
        #///////////////Вот эта переменная/////////////////
        self.text_show = tk.Text(self.dialog_frame,width=40, height=24)
        #//////////////////////////////////////////////////
        self.scroll = tk.Scrollbar(self.dialog_frame, command=self.text_show.yview)
        self.scroll.pack(side=tk.RIGHT, fill=tk.Y)
        self.text_show.config(yscrollcommand=self.scroll.set)
        self.text_show.configure(state='disabled')
        self.text_show.pack(side=tk.TOP) 
        self.dialog_frame.pack(side=tk.TOP)
        #////END SCROLLBAR////

        self.btn_dialog_sendmail.pack()
        self.text_enteremail.pack()

        self.btn_dialog_sendmail.place(x = 279,y = 400)
        self.text_enteremail.place(x = 29, y = 400)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-06-14
@Fizl

Try this line

self.text_show = tk.Text(self.dialog_frame,width=40, height=24)

Transfer to the __init__() function
You have follow_2 called before init_main() is called and text_show is defined only in it.
you can just move init_main() 2 lines higher.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question