简体   繁体   English

从GTK获得配色方案

[英]Get color scheme from GTK

I am developing a desktop app using Python 3 and GTK3. 我正在使用Python 3和GTK3开发桌面应用程序。

I need a button to change its background color when hover, which can be done with this: 我需要一个按钮来改变悬停时的背景颜色,可以这样做:

self.uploadbutton.modify_bg(Gtk.StateType.PRELIGHT, self.color)

Until now, I defined a RGB color that matches my own theme, but as I have to release this code, I would like to change that color to one of the established theme colors. 到目前为止,我定义了一个与我自己的主题相匹配的RGB颜色,但是由于我必须发布此代码,我想将该颜色更改为已建立的主题颜色之一。

Searching the web, I found there's a way to do it with GTK2 , but since version 3.8 of GTK, the gtk-color-scheme property has been deprecated . 在网上搜索,我发现有一种方法可以用GTK2来实现 ,但是从GTK的3.8版开始, gtk-color-scheme属性已被弃用

Is there any other way to respect the user theme colors without using this property? 有没有其他方法来尊重用户主题颜色而不使用此属性?

For now I have found two ways. 现在我找到了两种方法。 Both of them are not the answer to your question, because as far as I understand in GTK3 every single element can have it's own styling (including color). 它们都不是你问题的答案,因为据我所知,在GTK3中,每个元素都可以拥有自己的样式(包括颜色)。

The first one is official and says not to mess with themes unless you have tested them and know how exactly your modifications would look. 第一个是官方的,并说除非你测试过它们并且知道你的修改看起来究竟如何,否则不要弄乱主题。 I think it's the best solution since it's not clear at compile-time whether your self.color will make it look ok or totally unreadable. 我认为这是最好的解决方案,因为在编译时不清楚你的self.color是否会让它看起来不错或完全不可读。


The second solution is to obtain full CSS and parse it yourself. 第二种解决方案是获取完整的CSS并自行解析。

    gset = Gtk.Settings.get_default ()
    themename = gset.get_property ("gtk-theme-name")
    prefdark = gset.get_property ("gtk-application-prefer-dark-theme") # it's a boolean
    cprov = Gtk.CssProvider.get_named (themename)
    print (cprov.to_string())

Parsing the theme is outside of this question. 解析主题不在这个问题之内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM