Ammar
Ammar

Reputation: 26

How to run tkinter window parallel to other processes?

I have tkinter application that controlls as well as import values from a serial connection. I need two loops that run independently one for the serial connection and one for the main application of tkinter.

I tried doing that by using threading, from the answer in this link: Running infinite loops using threads in python but the tkinter application was running very slow. my code looked something like that.

import tkinter as tk
from threading import Thread

class Tkinter_Window(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()

    def run(self):
        self.tkinter_window = tk.Tk()
        self.tkinter_window.mainloop()

class Serial_Connection(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()

    def run(self):
        #serial connection should go here#
        pass

class Some_Other_Process(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()

    def run(self):
        #any other needed processes#
        pass


if __name__ == "__main__":
    Tkinter_Window()
    Serial_Connection()
    Some_Other_Process()
    while True:
        pass

however the tkinter application was too slow. after searching for the reason I read that multiprocessing might work better in my case. I appreciate any opinions about how to run parallel processes while being able to optimise how much processing power they take.

I also tried multiprocessing https://docs.python.org/2/library/multiprocessing.html but I couldn't figure out how to run the loop of the main tkinter application.

Upvotes: 0

Views: 1599

Answers (1)

rizerphe
rizerphe

Reputation: 1390

You can try doing something like using tkinter_window.update() regularly in your main code instead of mainloop. Hope that's helpful!

Upvotes: 1

Related Questions