-1

我想使用 MPD 的空闲功能等待任何更改,然后使用 Python 在 GTK GUI 中显示它们。问题是,当我使用 MPD 的空闲功能时,GUI 似乎阻塞并变得无响应(当更改歌曲时,GTK 窗口变得无响应)。当我删除self.mpd.idle()它时,它会起作用,但是该功能一直在运行,我认为这是不必要的。

解决这个问题的最佳方法是什么?

不工作 我最初的方法:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
from mpd import MPDClient

class GUI:
    def __init__(self):
        self.mpd = MPDClient()
        self.mpd.timeout = 10
        self.mpd.connect("localhost", 6600)

        self.window = Gtk.Window()
        self.window.connect("delete-event", Gtk.main_quit)
        self.window.show_all()

        GLib.idle_add(self.get_current_song)
        Gtk.main()

    def get_current_song(self):
        self.mpd.idle()
        print(self.mpd.currentsong())
        return True

app = GUI()

不工作我使用这个 的第二种方法。仍然得到相同的结果。

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
from mpd import MPDClient
import threading

class GUI:
    def __init__(self):
        self.mpd = MPDClient()
        self.mpd.timeout = 1
        self.mpd.connect("localhost", 6600)

        self.window = Gtk.Window()
        self.window.connect("delete-event", Gtk.main_quit)

        self.window.show_all()
        self.thread = threading.Thread(target=self.idle_loop)
        self.thread.daemon = True
        self.thread.start()
        Gtk.main()

    def get_songs(self):
        print(self.mpd.currentsong())
        self.mpd.idle()
        return True

    def idle_loop(self):
        GLib.idle_add(self.get_songs)

app = GUI()

工作 省略该GLib.idle_add()功能似乎是一种解决方案。但我不知道这是否是“正确”的方式。GLib.idle_add()不知道为什么弄乱它并且不使用它感觉不对,因为它在文档中提到。

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
from mpd import MPDClient
import threading

class GUI:
    def __init__(self):
        self.mpd = MPDClient()
        self.mpd.timeout = 1
        self.mpd.connect("localhost", 6600)

        self.window = Gtk.Window()
        self.window.connect("delete-event", Gtk.main_quit)

        self.window.show_all()
        self.thread = threading.Thread(target=self.get_songs)
        self.thread.daemon = True
        self.thread.start()
        Gtk.main()

    def get_songs(self):
        self.mpd.idle()
        print(self.mpd.currentsong())

app = GUI()
4

1 回答 1

0

Let us use the threading module here. As described in this article : https://pygobject.readthedocs.io/en/latest/guide/threading.html, we can make the following code:

import gi
from threading import Thread
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
from mpd import MPDClient

class GUI:
    def __init__(self):
        self.mpd = MPDClient()
        self.mpd.timeout = 10
        self.mpd.connect("localhost", 6600)

        self.window = Gtk.Window()
        self.window.connect("delete-event", Gtk.main_quit)
        self.window.show_all()

        thread = Thread(target=self.get_current_song, args=())
        thread.start()
        Gtk.main()

    def get_current_song(self):
        self.mpd.idle()
        print(self.mpd.currentsong())
        return True

app = GUI()
于 2020-01-29T15:43:24.933 回答