Tinkering with a small GTK3 app in Python3, I can trigger XDG theme sounds like so:
#!/usr/bin/python3
import gi
gi.require_version("Gtk", "3.0")
gi.require_version('GSound', '1.0')
from gi.repository import Gtk, GSound
class PyApp:
def __init__(self):
super(PyApp, self).__init__()
self.builder = Gtk.Builder()
self.builder.add_from_file("test.glade")
handlers = {
"onButtonClick": self.onButtonClick
}
self.builder.connect_signals(handlers)
self.sound = GSound.Context()
self.sound.init()
self.window = self.builder.get_object("window1")
self.window.show_all()
def onButtonClick(self, button):
self.sound.play_simple({ GSound.ATTR_EVENT_ID : "phone-incoming-call" })
if __name__ == "__main__":
app = PyApp()
Gtk.main()
But how do I make GSound play my own sounds? Do I have to construct a custom XDG theme, and if so do I have to install this in the correct system folder (e.g. /usr/share/sounds/
), or can I bundle it stand-alone with my app? Also, how do I tell the Gsound.context()
which theme to use? Very little information available about this.
Edit: This looked promising:
self.sound = GSound.Context()
self.sound.set_attributes({GSound.ATTR_CANBERRA_XDG_THEME_NAME: "freedesktop"})
Alas:
gi.repository.GLib.Error: gsound - error - quark: Invalid argument (-2)
question from:
https://stackoverflow.com/questions/65848724/how-to-play-custom-gui-sounds-in-gtk3-python-app 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…