在 python3 中将 PyGObject 中的线程与 GTK3 结合使用

Using threads in PyGObject with GTK3 in python3

所以我正在尝试使用线程在基于 Python3 的应用程序中实现阻塞操作。

#!/usr/bin/env python3

import gi, os, threading, Skype4Py
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, GObject

skype = Skype4Py.Skype()

def ConnectSkype():
    skype.Attach()

class Contacts_Listbox_Row(Gtk.ListBoxRow):

    def __init__(self, name):
        # super is not a good idea, needs replacement.
        super(Gtk.ListBoxRow, self).__init__()
        self.names = name
        self.add(Gtk.Label(label=name))

class MainInterfaceWindow(Gtk.Window):
    """The Main User UI"""

    def __init__(self):
        Gtk.Window.__init__(self, title="Python-GTK-Frontend")

        # Set up Grid object
        main_grid = Gtk.Grid()
        self.add(main_grid)

        # Create a listbox which will contain selectable contacts
        contacts_listbox = Gtk.ListBox()
        for handle, name in self.GetContactTuples():
            GLib.idle_add(contacts_listbox.add, Contacts_Listbox_Row(name))
        GLib.idle_add(main_grid.add, contacts_listbox)


        # Test label for debug
        label = Gtk.Label()
        label.set_text("Test")
        GLib.idle_add(main_grid.attach_next_to, label, contacts_listbox, Gtk.PositionType.TOP, 2, 1)

    def GetContactTuples(self):
        """
        Returns a list of tuples in the form: (username, display name).
        Return -1 if failure.
        """
        print([(user.Handle, user.FullName) for user in skype.Friends]) # debug
        return [(user.Handle, user.FullName) for user in skype.Friends]

if __name__ == '__main__':

        threads = []

        thread = threading.Thread(target=ConnectSkype) # potentially blocking operation
        thread.start()
        threads.append(thread)

        main_window = MainInterfaceWindow()
        main_window.connect("delete-event", Gtk.main_quit)
        main_window.show_all()
        print('Calling Gtk.main')
        Gtk.main()

基本思想是这个简单的程序应该从 Skype API 获取联系人列表,并构建元组列表。 GetContactTuples 函数设计成功,我发出的打印调用验证了这一点。但是,该程序无限期挂起,并且永远不会呈现界面。有时,它会产生涉及线程 and/or 资源可用性的随机错误。一旦这样的错误是

(example.py:31248): Gdk-WARNING **: example.py: Fatal IO error 11 (Resource temporarily unavailable) on X server :1.

我知道这与线程的使用有关,但根据文档here,似乎只需在接口更新之前添加GLib.idle_add 调用就足够了。所以问题是,为什么这不起作用,我该如何更正上面的示例?

更新: 如果 GLib.idle_add 被添加到与 GTK 交互的每一行之前,我会得到一个不同的错误。 [xcb] Unknown request in queue while dequeuing [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called [xcb] Aborting, sorry about that. python: xcb_io.c:179: dequeue_pending_request: Assertion '!xcb_xlib_unknown_req_in_deq' failed. Aborted (core dumped)

根据您的库版本(在 Gobject 3.10.2 中不再需要)您可能需要使用 GObject.threads_init() 显式初始化您的线程,如下所示:

if __name__ == '__main__':

    threads = []

    thread = threading.Thread(target=ConnectSkype) # potentially blocking operation
    thread.start()
    threads.append(thread)

    main_window = MainInterfaceWindow()
    main_window.connect("delete-event", Gtk.main_quit)

    GObject.threads_init()

    main_window.show_all()
    print('Calling Gtk.main')
    Gtk.main()