关于 python:Gtk3/Gnome 3 彩色按钮:应用”.needs-attention”css 样式

Gtk3/Gnome 3 colored button: apply ".needs-attention" css styles

前言

在 gnome 3 应用程序中,某些按钮通过有色背景而不是普通按钮的灰色背景突出显示。这些按钮不仅在使用标准 Adwaita 主题时颜色不同,而且在各种其他主题中也实现了。下面分别是 Adwaita 和 Flat Plat 主题的普通按钮和彩色按钮的示例。

阿德维塔

AdwaitaFlat

现在我的问题

我也希望能够在我的 Gtk3 应用程序中实现这些"重要按钮"。在研究如何做到这一点的过程中,我在主题文件中发现这些"重要按钮"有一个名为 needs-attention 的特殊 CSS 类。然后我尝试将按钮的 CSS 类也设置为 needs-attention。然而这并没有奏效。我仍然得到一个灰色的标准按钮。为了演示我做了什么,我附加了一个最小的脚本和正在运行的程序的屏幕截图。 "重命名" 按钮应该像上面的屏幕截图一样突出显示。我该怎么做?

我的代码

Screenshot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ButtonWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Needs Attention Button")
        self.set_border_width(10)

        hbox = Gtk.Box(spacing=6)
        self.add(hbox)

        button = Gtk.Button.new_with_label("Remove")
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button.new_with_mnemonic("Rename")
        button.get_style_context().add_class("needs-attention")
        hbox.pack_start(button, True, True, 0)

win = ButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

GNOME\\ 的 Adwaita 主题为建议的操作预定义了两个类:

  • suggested-action
  • destructive-action

您可以在 GNOME Wiki HowDoI/Buttons 上阅读:

While most buttons are gray, it has become more fashionable to
emphasize buttons that are the suggested action, by giving them a
different color. In the Adwaita theme, the color is blue. ...
Recently, another style class for dangerous actions has been added as
well: 'destructive-action'. The Adwaita theme uses red for buttons
marked as such.

在 Adwaita 上,它们分别如下所示:

  • enter
    1
    a€?needs-attentiona€?          gboolean

Sets a flag specifying whether the child requires the user attention.
This is used by the GtkStackSwitcher to change the appearance of the
corresponding button when a page needs attention and it is not the
current one.

可以在 gtk3-widget-factory 程序中看到生成的视觉辅助,作为需要注意的孩子上的蓝点(假设 Adwaita 主题):

enter

enter