I have this main thread:
Gui.py
from gi.repository import Gtk, Gdk
import Process
import gobject
class gui():
def __init__(self):
self.window = Gtk.Window()
self.window.connect('delete-event', Gtk.main_quit)
self.box = Gtk.Box()
self.window.add(self.box)
self.label = Gtk.Label('idle')
self.box.pack_start(self.label, True, True, 0)
self.progressbar = Gtk.ProgressBar()
self.box.pack_start(self.progressbar, True, True, 0)
self.button = Gtk.Button(label='Start')
self.button.connect('clicked', self.on_button_clicked)
self.box.pack_start(self.button, True, True, 0)
self.window.show_all()
gobject.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
def working1():
self.label.set_text('working1')
t = Process.Heavy()
t.heavyworks1()
self.label.set_text('idle')
def on_button_clicked(self, widget):
Gdk.threads_enter()
working1()
Gdk.threads_leave()
if __name__ == '__main__':
gui = gui()
This code will generate this gui: img http://i42.tinypic.com/33nvrx2.png
and I have second modul which will do the logic.
Process.py
import threading
class Heavy(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def heavyworks1(self):
#doing heavy works1
#return result
def heavyworks2(self, *param):
#doing heavy works2
#return result
When I execute this, the operation works, but the gui became freeze. How to do it well?
EDIT:
as user4815162342 said, I change my code to this:
from gi.repository import Gtk, Gdk, GLib
import Process
import gobject
import threading
class gui():
def __init__(self):
self.window = Gtk.Window()
self.window.connect('delete-event', Gtk.main_quit)
self.box = Gtk.Box()
self.window.add(self.box)
self.label = Gtk.Label('idle')
self.box.pack_start(self.label, True, True, 0)
self.progressbar = Gtk.ProgressBar()
self.box.pack_start(self.progressbar, True, True, 0)
self.button = Gtk.Button(label='Start')
self.button.connect('clicked', self.on_button_clicked)
self.box.pack_start(self.button, True, True, 0)
self.window.show_all()
gobject.threads_init()
GLib.threads_init()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
def init_progress(self, func, arg):
self.label.set_text('working1')
self.worker = threading.Thread(target=func, args=[arg])
self.running = True
gobject.timeout_add(200, self.update_progress)
self.worker.start()
def update_progress(self):
if self.running:
self.progressbar.pulse()
return self.running
def working(self, num):
Process.heavyworks2(num)
gobject.idle_add(self.stop_progress)
def stop_progress(self):
self.running = False
self.worker.join()
self.progressbar.set_fraction(0)
self.label.set_text('idle')
def on_button_clicked(self, widget):
self.init_progress(self.working, 100000)
if __name__ == '__main__':
gui = gui()
with that code, program sometimes working but sometimes getting this error.
1.
**
Gtk:ERROR:/build/buildd/gtk+3.0-3.4.2/./gtk/gtktextview.c:3726:gtk_text_view_validate_onscreen: assertion failed: (priv->onscreen_validated)
Aborted (core dumped)
2.
*** glibc detected *** python: free(): invalid next size (fast): 0x09c9f820 ***
3.
Segmentation fault (core dumped)