I
I
ismail_20072021-03-31 14:15:53
Python
ismail_2007, 2021-03-31 14:15:53

How can you run two functions in parallel in this program?

Hi all! I'm trying to run the mail_thread() function in parallel with the rest of the program. I already had attempts to do this, but always either only the window worked, and then the function worked, or the function worked, and the window did not open at all. Any ideas how to implement this?
Here is the main window class code:

class Main(tk.Frame):
    def __init__(self, root, user_name):
        super().__init__(root)
        self.user_name = user_name
        self.db = db
        self.db.cur = self.db.conn.cursor()
        self.db.cur2 = self.db.conn2.cursor()
        self.init_main()
        self.get_mail_class_start = get_mail_class_start
        self.init_mail_thread = threading.Thread(target = self.mail_thread, args = ())
        self.init_mail_thread.start()

 
    def init_main(self):
 
        btn_database_workers = tk.Button(root, padx = 20, pady = 1, text = 'WORKER', bg = '#2A3132',fg = '#336B87',
                                         activebackground = '#2A3132', relief = tk.FLAT, activeforeground = '#336B87',
                                         compound = tk.TOP, font = ('COMIC SANS MS', 60), command = self.open_database)
        
        btn_database_workers.pack()
        btn_database_workers.place(x=150, y=20)
 
        worker_lbl = tk.Label(text = 'Работники', bg = '#2A3132', fg = '#336B87', font = ('COMIC SANS MS',20))
        worker_lbl.pack()
        worker_lbl.place(x = 50,y = 160)
 
        worker_lbl2 = tk.Label(text = 'Задания', bg = '#2A3132', fg = '#336B87', font = ('COMIC SANS MS',20))
        worker_lbl2.pack()
        worker_lbl2.place(x = 530,y = 160)
 
        scframe = VerticalScrolledFrame(root)
        scframe.pack()
        scframe.place(x = 20,y = 200)
 
        scframe2 = VerticalScrolledFrame2(root)
        scframe2.pack()
        scframe2.place(x = 500,y = 200)
 
        for value in self.db.cur.execute("SELECT name FROM tbl"):
            massive = list(value)
            btn = tk.Button(scframe.interior, height=2, width=35, relief=tk.FLAT, 
                        bg='#336B87', activebackground = '#2A3132', activeforeground = '#336B87', fg = '#2A3132',
                        font=("COMIC SANS MS", 0), text=massive[0],command = partial(self.dialog, massive[0]))
            btn.pack(padx=10, pady=5, side=tk.TOP)
 
        for value2 in self.db.cur2.execute("SELECT task_name FROM task"):
            massive2 = list(value2)
            btn2 = tk.Button(scframe2.interior, height = 2, width = 35, relief = tk.FLAT, 
                        bg = '#336B87', activebackground = '#2A3132',activeforeground = '#336B87', fg = '#2A3132',
                        font = ("COMIC SANS MS", 0), text = massive2[0], command = partial(self.taskmenu, massive2[0]))
            btn2.pack(padx=10, pady=5, side=tk.TOP)
 
 
    def open_database(self):
        Child()
 
    def dialog(self, user_name):
        DialogMenu(user_name)
 
    def taskmenu(self, task_name):
        TaskMenu(task_name)
    
    
    def mail_thread(self):
        time.sleep(1)
        self.after(True,get_mail_class_start.get_mail_func())

Here is the dialog box code:
class DialogMenu(tk.Toplevel):
    def __init__(self,user_name):
        super().__init__(root)
        self.user_name = user_name
        self.db = db
        self.get_mail_class_start = get_mail_class_start
        self.send_mail = Mail_class(user_name, smtplib.SMTP('smtp.gmail.com:587'))
        self.db.cur = self.db.conn.cursor()
        self.init_dialog_thread = threading.Thread(target = self.init_dialog, args =())
        self.init_dialog_thread.start()




    def retrieve_message_input(self):
        self.messsage_text = self.Message_Text.get(1.0, tk.END)
        self.send_mail.send_mail(self.messsage_text)
 
    def init_dialog(self):
        
        self.geometry('600x550')
        self.resizable(False,False)
        self['bg'] = '#2A3132'
        self.minsize(width = 900, height = 550) 
        self.title(f'Диалог с {self.user_name}')
 
        server = 'smtp.gmail.com:587'
        self.send_mail.start_client(server)
        
        self.Send_btn = tk.Button(self,text = '',padx=15,pady=12.5,bd=5,font=('Ubuntu',35),
                             bg = '#336B87',command = partial(self.dialog_db,self.user_name))
 
        self.Send_btn_sms = tk.Button(self,text = '➤',padx = 15,pady = 13,font = ('Ubuntu',35),bd = 5,
                                      bg = '#336B87', command = self.retrieve_message_input)
 
        self.Message_Text = tk.Text(self,width = 45,height = 5,bg='white',fg='black',font = 50)
        self.frame = tk.Frame(self)
        self.text = tk.Text(self.frame, width=85, height=22)
        self.scroll = tk.Scrollbar(self.frame, command=self.text.yview)
        self.scroll.pack(side=tk.RIGHT, fill=tk.Y)
        self.text.config(yscrollcommand=self.scroll.set)
        self.text.insert(tk.INSERT,("text" + '\n') * 50)
        self.text.configure(state='disabled')
 
        self.text.pack(side=tk.TOP) 
        self.text.place()
 
        self.frame.pack(side=tk.TOP)
        self.Send_btn.pack()
        self.Send_btn_sms.pack()
        self.Message_Text.pack()
 
        self.Send_btn.place(x = 670,y = 400)
        self.Send_btn_sms.place(x = 545, y = 400)
        self.Message_Text.place(x = 34,y = 400)
  
    def dialog_db(self, user_name):
        DialogDB(user_name)


Here is the last part of the code:
if __name__ == '__main__':
    
    get_mail_class_start = get_mail_class(imaplib.IMAP4_SSL('imap.gmail.com') )
    root = tk.Tk()
    db = DB()
    app = Main(root, '***********')
    root['bg'] = '#2A3132'

    app.pack()
    root.title('Main')
    root.geometry('1000x700')
    root.resizable(False,False)
    root.mainloop()


The function inside mail_thread should run indefinitely.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question