Feb-06-2021, 09:23 PM
(This post was last modified: Feb-06-2021, 09:23 PM by giorgosmarga.)
I have 2 scripts scriptA and scriptB.Using multithreading i want to call scriptA and print sometihng on the terminal and I want to get that something and use it as a text in my gui . How to do it? So far i've tried this
class DisplaySubprocessOutput: def __init__(self, root): self.root = root self.process = Popen( [sys.executable, "-u", "-c", dedent(""" from threading import Thread from giveaway_bot import main main() """)], stdout=PIPE) q = Queue(maxsize=1024) t = Thread(target=self.reader_thread, args=[q]) t.daemon = True # close pipe if GUI process exits t.start() # show subprocess' stdout in GUI self.label = tk.Label(root, text=" ", fg="Red", bg="White", font=("Arial", 10), width=350, anchor="w") self.label.place(x=0, y=300) self.update(q) # start update loop def reader_thread(self, q): """Read subprocess output and put it into the queue.""" try: with self.process.stdout as pipe: for line in iter(pipe.readline, b''): q.put(line) finally: q.put(None) def update(self, q): """Update GUI with items from the queue.""" for line in iter_except(q.get_nowait, Empty): # display all content if line is None: return else: self.label['text'] = line # update GUI break # display no more than one line per 40 milliseconds self.root.after(40, self.update, q) def quit(self): self.process.kill() # exit subprocess if GUI is closed (zombie!) self.root.destroy()but it doesnt work because i have to wait for main() to run.Its not neccessary to use threading