GTK3重用CSS状态背景色 [英] GTK3 reuse CSS state background colors
问题描述
我正在移植PyGTK2应用程序.
I am porting a PyGTK2 app.
此应用执行以下操作:
self.normal_color = editor.style.base [Gtk.STATE_NORMAL]
self.insensitive_color = editor.style.base [Gtk.STATE_INSENSITIVE]
然后进行设置:
if editable: self.editor.modify_base (Gtk.STATE_NORMAL, self.normal_color)
else: self.editor.modify_base (Gtk.STATE_NORMAL, self.insensitive_color)
self.editor
是GtkTextView
.之所以不将其设置为editor.set_sensitive(False)
,是因为编辑器中的用户程序,并且在加载其代码时,编辑器仍必须是可选择的,我们不希望文本变灰.
self.editor
is a GtkTextView
. The reason why it does not just set it to editor.set_sensitive(False)
is because the user programs in the editor and when loading his code the editor must still be selectable and we do not want the text grayed-out.
这是我使用CSS的端口的MWE:
Here is a MWE of my port using CSS:
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk
view = Gtk.TextView()
view.set_name('editable')
scroll = Gtk.ScrolledWindow()
scroll.add(view)
def button_clicked(button):
edit = not view.get_editable()
view.set_name('editable' if edit else 'readonly')
button.set_label('Load' if edit else 'Edit')
view.set_editable(edit)
button = Gtk.Button('Load')
button.connect('clicked', button_clicked)
box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
box.pack_start(scroll, True, True, 0)
box.pack_start(button, False, True, 0)
window = Gtk.Window()
window.set_default_size(300, 300)
window.add(box)
window.show_all()
window.connect('delete_event', Gtk.main_quit)
css = Gtk.CssProvider()
css.load_from_path('test.css')
style = Gtk.StyleContext()
style.add_provider_for_screen(Gdk.Screen.get_default(), css,
Gtk.STYLE_PROVIDER_PRIORITY_USER)
Gtk.main()
以及相应的CSS文件:
And the respective CSS file:
#editable {
background-color: white;
/*background-color: @normal_bg_color;*/
}
#readonly {
background-color: @insensitive_bg_color;
}
也就是说,我使用CSS名称来动态更改背景颜色.自从似乎一切都已弃用以来,我还没有找到一种更简单的编程方式.
That is, I use CSS names to dynamically change the background colors. I have not found a simpler programatically way since everything seems deprecated.
我的问题是,无论出于什么原因,@normal_bg_color
对于GtkTextView都不是白色,这很奇怪.
My problem is that for whatever reason @normal_bg_color
is not white for GtkTextView which is odd.
我猜@*_bg_color
是通用颜色,并不特定于窗口小部件.还是我错了?
I guess @*_bg_color
are generic colors, and not specific to the widget. Or am I wrong?
我想要一些@GtkTextView:normal_bg_color
...
view.get_style_context().get_background_color()
已被弃用.是否有可能获得用户的主题颜色?
view.get_style_context().get_background_color()
has been deprecated. Is there any possibility of getting the user's theme colors?
推荐答案
出于历史原因,您要查找的颜色可能是@base_color
. GTK始终在基本"颜色(用于文本输入小部件)和背景"颜色(用于其他所有颜色)之间进行区分.
The color you're looking for is probably @base_color
, for historical reasons. GTK has always had a distinction between "base" colors (for text entry widgets) and "background" colors (for everything else.)
这还不是文档,但是可以在此页面上找到示例. .
It's not quite documentation, but examples are available on this page.
这篇关于GTK3重用CSS状态背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!