gtk ui block

Emitting signals from a thread will run the handler in the same thread. You shouldn't call gtk functions from a thread other than the main one. If you insist on doing so you have to make sure to acquire the GDK lock first by wrapping your code in gtk.gdk.threads_enter() ... gtk.gdk.threads_leave() calls. In this case you also have to call gtk.gdk.thread_init() as otherwise the main loop will never release the GDK lock. The easier solution is to push the signal emission to the main thread by replacing self.emit('ASignal') with gobject.idle_add(self.emit, 'ASignal') which guarantees the handler will also run in the main thread (which already has the GDK lock).

Regards.


On Thu, Jul 25, 2013 at 4:56 PM, Todong Ma  <[hidden email]> wrote:
Hi, everyone

I met a problem that UI will be blocked forever if I emit a signal from a thread to create a message dialog.
Following code will reproduce the issue, please let me give a short explanation: AThread thread defines a custom signal named "ASignal", and the signal handler is aSignal() function of MainWindow class. The aSignal() function is used to display a message dialog. Emitting "ASignal" signal will display the message dialog successfully but the UI is blocked forever.
import gtk
import gobject
import threading

class AThread(threading.Thread, gobject.GObject):
    __gsignals__ = {
        'ASignal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
    }
    def __init__(self):
        threading.Thread.__init__(self)
        gobject.GObject.__init__(self)

    def run(self):
        self.emit('ASignal')

class MainWindow(gtk.Window):
    def aSignal(self, obj):
        dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, 'asdfasf')
        dialog.run()
        dialog.destroy()

gobject.threads_init()

w = MainWindow()

t = AThread()
t.connect('ASignal', w.aSignal)
t.start()

gtk.main()

Any suggestion is appreciated.

Thanks,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值