PyGtk3 how to change application theme?

偶尔善良 提交于 2019-12-06 14:57:45

问题


i tried rc_parse method using Gtk.rc_parse("path/to/Hooli/gtk-3/gtk.css") code but it is not working. this question is simlpy but solution of question not easy. how can i change the my gtk apllication theme ? or how can i use custom theme on my gtk application ?

Thanks.


回答1:


import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk

settings = gtk.Settings.get_default()
settings.set_property("gtk-theme-name", "Numix")
settings.set_property("gtk-application-prefer-dark-theme", False)  # if you want use dark theme, set second arg to True

# getting all existing properties #

for i in settings.list_properties():
    print(i)

this codes working very correctly on windows, linux, ... i hope this solve your problem




回答2:


You are mixing the old way with the new CSS way.

I wrote a few examples that are in the demos folder of the pygobject tree, the most simple one is this one:

https://git.gnome.org/browse/pygobject/tree/demos/gtk-demo/demos/Css/css_accordion.py

In short you have to prepare the css:

https://git.gnome.org/browse/pygobject/tree/demos/gtk-demo/demos/data/css_accordion.css

The file is loaded into a Gtk.CssProvider()

    provider = Gtk.CssProvider()
    provider.load_from_data(bytes.get_data())

Which is loaded by the function apply_css()

def apply_css(self, widget, provider):
    Gtk.StyleContext.add_provider(widget.get_style_context(),
                                  provider,
                                  Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    if isinstance(widget, Gtk.Container):
        widget.forall(self.apply_css, provider)

Run the full example to get a better idea of how it works.



来源:https://stackoverflow.com/questions/42881112/pygtk3-how-to-change-application-theme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!