#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Mar 18 18:06:34 2021 @author: angoosh """ import threading #from multiprocessing import Process, Queue import queue import gi from time import sleep gi.require_version('Gtk', '3.0') gi.require_version('GLib', '2.0') from gi.repository import Gtk, GLib IsEnded = 0 counter = 0 def do_work(com_queue): global counter while True: counter += 1 com_queue.put(str(counter)) sleep(0.1) print("A") if IsEnded != 0: break # continue class MainGUI(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Append") self.com_queue = queue.Queue() self.worker_thread = None self.liststore = None self.box = Gtk.Box(spacing=6) self.add(self.box) self.textArea = Gtk.TextView() self.textbuffer = self.textArea.get_buffer() self.textbuffer.set_text("Ready: \n") self.box.pack_start(self.textArea, True, True, 0) self.button1 = Gtk.Button(label="Start") self.button1.connect("clicked", self.butt) self.box.pack_start(self.button1, True, True, 0) self.button2 = Gtk.Button(label="Stop") self.button2.connect("clicked", self.butt2) self.box.pack_start(self.button2, True, True, 0) self.tid = None self.proc = None #self.state = mp.Value('i', 0) # more gui initialization... def launch_worker_thread(self): #self.proc = Process(target=do_work, args=(self.com_queue,)) #self.proc.start() self.worker_thread = threading.Thread(target=do_work, args=(self.com_queue,)) self.worker_thread.start() try: self.tid = GLib.timeout_add(1, self.check_queue, None) # run check_queue every 1 second except: print("Error") def butt(self, widget): self.launch_worker_thread() def butt2(self, widget): global IsEnded IsEnded = 1 def prn(self, ignored): print("B") GLib.idle_add(self.update_treeview, "update") def check_queue(self, ignored): if self.tid is not None: #if self.worker_thread.is_alive(): try: update = self.com_queue.get(False) GLib.idle_add(self.update_treeview, update) # send tuple except queue.Empty: pass return True # to keep timeout running else: return False # to end timeout def update_treeview(self, update): text_end = self.textbuffer.get_end_iter() self.textbuffer.insert(text_end, str(update)) # here update the treeview model with tuple if __name__ == "__main__": gui = MainGUI() gui.connect("destroy", Gtk.main_quit) gui.show_all() Gtk.main() IsEnded = 1