2

我正在尝试设置 GtkSourceView 对象的样式,特别是我想更改行号的颜色。我正在阅读https://developer.gnome.org/gtksourceview/stable/style-reference.html。我可以改变text,但不能line-numbers。文件说:

line-numbers :左边距的文本和背景颜色,在其上绘制行号。

这是来源:

#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>

int main (int argc,
char *argv[])
{
    GtkWidget *window, *scrolled_win, *textview;
    GtkWidget *vbox, *menubar, *fileMenu, *fileMi, *quit_item;

    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_maximize(GTK_WINDOW (window));
    vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    gtk_window_set_title (GTK_WINDOW (window), "Text Views");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    textview = gtk_source_view_new();
    menubar = gtk_menu_bar_new();
    fileMenu = gtk_menu_new();

    fileMi = gtk_menu_item_new_with_label("File");

    quit_item = gtk_menu_item_new_with_label("Quit");

    gtk_widget_set_hexpand(textview, TRUE);
    gtk_widget_set_vexpand(textview, TRUE);

    GtkCssProvider *provider = gtk_css_provider_new ();
    gtk_css_provider_load_from_path (provider,
    "gtk-widgets.css", NULL);

    GtkStyleContext *context;
    context = gtk_widget_get_style_context(textview);
    gtk_style_context_add_provider (context,
                                    GTK_STYLE_PROVIDER(provider),
                                    GTK_STYLE_PROVIDER_PRIORITY_USER);

    gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quit_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
    gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);

    scrolled_win = gtk_scrolled_window_new (NULL, NULL);
    gtk_container_add(GTK_CONTAINER(vbox), scrolled_win);
    gtk_container_add (GTK_CONTAINER (scrolled_win), textview);

    gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW(textview), TRUE);
    gtk_widget_show_all (window);

    g_signal_connect(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), NULL);

    g_signal_connect(G_OBJECT(quit_item), "activate",
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_main();
    return 0;
}

这是 CSS 文件:

text {
    background-color: #002b36;
    color: #c7a21d;
}

line-numbers {
    background-color: red;
    color:red;
}

current-line-number {
    background-color: red;
    color:red;
}

selection{
    color:yellow;
}

我究竟做错了什么?

4

1 回答 1

0

API有一种访问默认样式的方法,如下所示:

manager = GtkSource.StyleSchemeManager().get_default()

print (manager.get_scheme_ids())
>['classic', 'cobalt', 'kate', 'oblivion', 'solarized-dark', 'solarized-light', 'tango']

scheme = manager.get_scheme("oblivion")
buffer.set_style_scheme(scheme)

一个很好的衡量标准:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
from gi.repository import GtkSource as gtksource
from gi.repository import Pango

view = gtksource.View()
view.modify_font(Pango.FontDescription('monospace 11'))
view.set_show_line_numbers(True)
view.set_show_line_marks(True)
buffer = view.get_buffer()
filename = "treeview.py"
txt = open(filename).read()
buffer.set_text(txt)
# set language, syntax highlighting
lm = gtksource.LanguageManager.new()
lang = lm.guess_language(filename)
buffer.set_highlight_syntax(True)
buffer.set_language(lang)
buffer.create_tag("invisible",invisible=True)
manager = gtksource.StyleSchemeManager().get_default()
scheme = manager.get_scheme("monokai")
buffer.set_style_scheme(scheme)
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.add(view)
window.show_all()
gtk.main()

输出

从那里您应该可以使用该指南应用自定义主题、更改某些元素的颜色等...

于 2020-08-11T04:40:05.547 回答