我正在使用 gtk3 创建 GUI。为了使 GUI 和操作一起工作,我使用以下代码创建了一个线程threading.Thread(target=function).start()
:没有线程,一切正常,但 GUI 将被冻结。使用线程,发生此错误:
第一个是Segmentation fault (core dumped)
第二个是*** glibc detected *** python: double free or corruption (!prev): 0x09154320 ***
第三个是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)
你知道为什么会这样吗?
编辑: 我的代码:
图形用户界面.py
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()
GLib.threads_init()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
def working1(self):
self.label.set_text('working1')
result = Process.heavyworks1()
print result
self.label.set_text('idle')
def on_button_clicked(self, widget):
threading.Thread(target=self.working1).start()
if __name__ == '__main__':
gui = gui()
进程.py
a = 0 x = '某事'
def heavyworks1():
#global a
#doing something
#doing other thing
#a = something
#return result
def heavyworks2(param):
#doing something
#doing other thing
#return result