簡體   English   中英

在python3中的GTK3中使用PyGObject中的線程

[英]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函數在其設計中成功,我放置的打印調用驗證了這一點。 但是,該程序會無限期掛起,並且永遠不會呈現接口。 有時,它會產生涉及線程和/或資源可用性的隨機錯誤。 一旦這樣的錯誤是

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

我知道它與線程的使用有關,但是根據這里的文檔,似乎只是在接口更新之前添加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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM