如何仅更改一个选项卡的颜色 GtkNotebook

How to change color only one tab GtkNotebook

我正在使用 GTK 版本 3.10.8 开发 Ubuntu 14。我现在不想升级到 Ubuntu 16…….

尽管如此,我对 CSSGtkNoteBook

的理解有疑问

我的测试

是否可以只更改一个选项卡的颜色,例如

GtkNotebook tab:nth-child(3) {} or
GtkNotebook tab:nth-child(4){} or
GtkNotebook tab:nth-child(5) etc …..

我觉得还行还是不行

/* 先ok / / 最后确定 */

/* 奇数 ok */

/*还可以*/

/* 1 好 */

/* 2n+1 好 */

/* 3n+4 不行 */

/* 2 不正常 */

/* 一个不行 */

/* 一个不行 */

#!/usr/bin/python
# -*- coding: ISO-8859-1 -*-
# notebook_20.py
from gi.repository import Gtk, Gdk

def _destroy_cb(widget, data=None):
    Gtk.main_quit()

window = Gtk.Window()
window.connect("destroy", _destroy_cb)

screen = Gdk.Screen.get_default()

css_provider = Gtk.CssProvider()

css = """
        /* Theme any label within a notebook */
        GtkNotebook tab GtkLabel {background-color: green;color: cyan;}
        GtkNotebook > GtkLabel {background-color: pink;color: cyan;}


        GtkNotebook {              /*------  OR use " GtkNotebook#notebook OR GtkNotebook.mynotebook  */ 
        -GtkWidget-focus-line-width: 0;    /* Remove focus line (dotted line) around text on all tabs */
        -GtkNotebook-tab-overlap: 0; 
        padding: 10px 20px 10px 20px;
        border-radius: 15px; 
        border-width: 4px;               
        }   


        GtkLabel.first_label {        /* another way to select tab label */
        color: green;                 /* overides last font color selection  */
        font: Serif italic 10;        /* overides last font & font size selection */
        }

        /* first ok */
        /* last ok  */
        /* odd ok  */
        /* even ok  */
        /* 1 ok  */
        /* 2n+1 ok  */
        /* 3n+4 non ok  */
        /* 2 non ok  */
        /* one non ok  */
        /* one non ok  */

        GtkNotebook tab:nth-child(3n+4) {  /* modification 1er onglet tab "FirstTab */
        background-color: pink;             /* tab background color */
        color: green;                         /* tab font color */
        font: Sans 12;                      /* tab font & font size */
        }
        """
css_provider.load_from_data(css)


context = Gtk.StyleContext()
context.add_provider_for_screen(screen, css_provider,
                                Gtk.STYLE_PROVIDER_PRIORITY_USER)

box = Gtk.VBox()
window.add(box)

notebook = Gtk.Notebook()
box.pack_start(notebook, False, False, 0)

for i in range(10):
    bufferf = "Prepend Frame %d" % (i+1)
    bufferl = "Page %d" % (i+1)

    frame = Gtk.Frame()
    frame.set_border_width(10)
    frame.set_size_request(100, 75)
    label = Gtk.Label(bufferf)
    frame.add(label)
    label.show()

    label2 = Gtk.Label(bufferl)
    notebook.append_page(frame, label2)
    frame.show()

window.show_all()
Gtk.main()

你能帮帮我吗 也许我必须更改 gtk 版本(现在我有版本 3.10.8 ) 提前致谢

您可以在 CSS 字符串中添加一个特殊的 CSS ID:

    #page2 {
        background-color: #0f0;
    }

然后 set_name 用于第 2 页的 "page 2"。例如在创建单个页面的 for 循环中:

for i in range(10):
    ...
    label2 = Gtk.Label(bufferl)
    if i == 2:
        label2.set_name('page2')
    notebook.append_page(frame, label2)
    frame.show()

好的,非常感谢您的帮助。我的理解是: - GtkNotebook tab:nth-child(3) {} 在 CSS3 规范中是 "implemented",但是 - 未在 Gtk 3 版本 3.10.8 中实现。

事实上我改变了我的做法。 首先我想管理一个笔记本。 其次,当我在选项卡子项中更改日期时,我想指示状态已修改 如果数据未更改状态未修改 经过一两个晚上!!!,我发现我只需要管理两个状态。

我的解决方案是:

if status == 'modified';
    label.set_name ('modified)
else
    label.set_name ('no_modified)
.......

GtkNotebook > #no_modified {background-color: green;}
GtkNotebook > #modified {background-color: red;}

或类似的东西。 第二天我要测试这个。 非常感谢你