Answer the question
In order to leave comments, you need to log in
How to use a variable inside a class (not an attribute) outside of it?
Hello! I need to specify a variable body
inside the class, which is not its attribute, outside of it. How to do it?
Here is the class code where this variable is located:
class get_mail_class():
def __init__(self, mail):
self.mail = mail
self.email_file = codecs.open('email.txt', 'r')
self.password_file = codecs.open('password.txt', 'r')
self.read_email_file = str(self.email_file.read())
self.read_password_file = str(self.password_file.read())
(self.retcode, self.capabilities) = self.mail.login(self.read_email_file, self.read_password_file)
def get_mail_func(self):
my_file = open('text.txt', 'a+')
self.mail.list()
self.mail.select('inbox')
n=0
(retcode, messages) = self.mail.search(None, '(UNSEEN)')
if retcode == 'OK':
for num in messages[0].split():
n=n+1
typ, data = self.mail.fetch(num,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
original = email.message_from_bytes(response_part[1])
raw_email = data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
typ, data = self.mail.store(num,'+FLAGS','\\Seen')
if email_message.is_multipart():
for payload in email_message.get_payload():
body = payload.get_payload(decode=True).decode('utf-8')
print(original['From'])
print(body)
#self.dialog.text.configure(state = 'normal')
#self.dialog.text.insert(tk.INSERT, body)
#self.dialog.text.configure(state = 'disabled')
my_file.write((f"\n {body}"))
else:
body = email_message.get_payload(decode=True).decode('utf-8')
print(original['From'])
print(body)
my_file.write((f"\n {body}"))
def main_dialog_thread():
thread_ins = threading.Thread(target = dialog_class.insert_mail, args = ('''Здесь должна быть переменная body''', ), daemon = True)
thread_ins.start()
Answer the question
In order to leave comments, you need to log in
body = email_message.get_payload(decode=True).decode('utf-8')
class get_mail_class():
body = None
.....
get_mail_class.body = email_message.get_payload(decode=True).decode('utf-8')
....
def main_dialog_thread():
thread_ins = threading.Thread(target = dialog_class.insert_mail, args = (get_mail_class.body), daemon = True)
thread_ins.start()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question