问题
I am porting a PyGTK2 app.
This app does things like this:
self.normal_color = editor.style.base [Gtk.STATE_NORMAL]
self.insensitive_color = editor.style.base [Gtk.STATE_INSENSITIVE]
And sets it afterwards:
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
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.
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()
And the respective CSS file:
#editable {
background-color: white;
/*background-color: @normal_bg_color;*/
}
#readonly {
background-color: @insensitive_bg_color;
}
That is, I use CSS names to dynamically change the background colors. I have not found a simpler programatically way since everything seems deprecated.
My problem is that for whatever reason @normal_bg_color
is not white for GtkTextView which is odd.
I guess @*_bg_color
are generic colors, and not specific to the widget. Or am I wrong?
I would like to have some @GtkTextView:normal_bg_color
...
view.get_style_context().get_background_color()
has been deprecated. Is there any possibility of getting the user's theme colors?
回答1:
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.
来源:https://stackoverflow.com/questions/38158289/gtk3-reuse-css-state-background-colors