在一些程序中,黑色标题小部件是什么?

在一些Ubuntu程序(如Ubuntu控制面板、系统设置)中,但不包括Banshee,在窗口的顶部部分使用了深色调元素(使用Ambience主题)。但我找不到一个能自动实现这个效果的标准小部件。
这些颜色是手动设置的吗(而不是使用标准小部件+主题)?如果是手动设置的,那么在主题中它们是从哪里来的(gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, &color)中的参数是什么)?
编辑:看起来这并不是一个简单的Gtk.Toolbar。如果我运行以下代码:
from gi.repository import Gtk
window = Gtk.Window()
window.set_default_size(200, -1)
window.connect("destroy", lambda q: Gtk.main_quit())
toolbar = Gtk.Toolbar()
window.add(toolbar)
toolbutton = Gtk.ToolButton(stock_id=Gtk.STOCK_NEW)
toolbar.add(toolbutton)
window.show_all()
Gtk.main()

我得到了一个像这样的窗口: enter image description here 它的工具栏没有暗色调。
编辑2:尽管j-johan-edwards提供的“具有特殊上下文工具栏”的答案在大多数程序中都是正确的,但在ubuntuone-control-panel中并非如此。该程序具有可以包含任何范围小部件的GtkVBox(而不是工具栏)。我仍然无法确定gtk主题如何知道如何绘制窗口的那部分。 enter image description here 但无论如何:现在对我来说工具栏就足够了...
2个回答

你是指这些吗?

GTK3 Toolbar

它们只是 Gtk.Toolbar。像Banshee这样的一些应用程序之所以不使用它们,是因为它们尚未迁移到 GTK+ 3 并获得了新的主题能力,使得像那样的工具栏成为可能。

要将您自己的Python应用程序迁移到GTK+ 3,您需要使用 PyGObject 而不是PyGTK。从12.04版本开始,Quickly 将默认生成PyGObject项目。

您还需要将primary-toolbar添加到工具栏样式上下文中。如下所示:

toolbar = Gtk.Toolbar()
context = toolbar.get_style_context()
context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)

将这个背景应用到问题示例中,结果如下:

demo


我添加了一个不像暗色的 Gtk.Toolbar 的示例。所以我猜它不是一个简单的 Gtk.Toolbar? - xubuntix
Gtk.get_major_version() 返回的是 3,但是我仍然得到的是旧的工具栏。这是在 Python2 和 Python3 中都执行了 from gi.repository import Gtk 之后出现的情况。 - Stefano Palazzo
1你贴出的链接中的PyGI演示也没有它们。也许应用程序开发人员需要自行添加样式? - Stefano Palazzo
谢谢!早上一直在头疼,感觉像撞墙一样。应该先在这里搜索的。 - squareborg

关于你的问题的第二部分,即“如何将VBox添加到工具栏”,你只需要将它包装在一个Gtk.ToolItem中即可。
...
self.toolbar = Gtk.Toolbar()
self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
tool_item = Gtk.ToolItem()
tool_item.add(self.box)
self.toolbar.insert(tool_item, 0)
...

你可以通过创建一个辅助函数或者扩展Gtk.Toolbar来简化它,例如:

custom_toolbar.py

from gi.repository import Gtk

class CustomToolbar(Gtk.Toolbar):
    def __init__(self):
        super(CustomToolbar, self).__init__()
        ''' Set toolbar style '''
        context = self.get_style_context()
        context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)

    def insert(self, item, pos):
        ''' If widget is not an instance of Gtk.ToolItem then wrap it inside one '''
        if not isinstance(item, Gtk.ToolItem):
            widget = Gtk.ToolItem()
            widget.add(item)
            item = widget

        super(CustomToolbar, self).insert(item, pos)
        return item

它只是检查您尝试插入的对象是否为ToolItem,如果不是,则将其包装在其中。使用示例:

main.py

#!/usr/bin/python
from gi.repository import Gtk
from custom_toolbar import CustomToolbar

class MySongPlayerWindow(Gtk.Window):
    def __init__(self):
        super(MySongPlayerWindow, self).__init__(title="My Song Player")
        self.set_size_request(640, 480)

        layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(layout)

        status_bar = Gtk.Statusbar()
        layout.pack_end(status_bar, False, True, 0)

        big_button = Gtk.Button(label="Play music")
        layout.pack_end(big_button, True, True, 0)

        ''' Create a custom toolbar '''
        toolbar = CustomToolbar()
        toolbar.set_style(Gtk.ToolbarStyle.BOTH)        
        layout.pack_start(toolbar, False, True, 0)

        ''' Add some standard toolbar buttons '''
        play_button = Gtk.ToggleToolButton(stock_id=Gtk.STOCK_MEDIA_PLAY)
        toolbar.insert(play_button, -1)

        stop_button = Gtk.ToolButton(stock_id=Gtk.STOCK_MEDIA_STOP)
        toolbar.insert(stop_button, -1)

        ''' Create a vertical box '''
        playback_info = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, margin_top=5, margin_bottom=5, margin_left=10, margin_right=10)

        ''' Add some children... '''
        label_current_song = Gtk.Label(label="Artist - Song Name", margin_bottom=5)
        playback_info.pack_start(label_current_song, True, True, 0)

        playback_progress = Gtk.ProgressBar(fraction=0.6)
        playback_info.pack_start(playback_progress, True, True, 0)

        '''
        Add the vertical box to the toolbar. Please note, that unlike Gtk.Toolbar.insert,
        CustomToolbar.insert returns a ToolItem instance that we can manipulate
        '''
        playback_info_item = toolbar.insert(playback_info, -1)
        playback_info_item.set_expand(True)        

        ''' Add another custom item '''       
        search_entry = Gtk.Entry(text='Search')
        search_item = toolbar.insert(search_entry, -1)
        search_item.set_vexpand(False)
        search_item.set_valign(Gtk.Align.CENTER)

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

它应该看起来像this


1就记录而言,你也可以用glade来做这个。GtkToolItem在小部件面板中不可见。相反,你需要右键单击对象树中的GtkToolbar并选择“编辑”以打开一个单独的编辑窗口。转到“层次结构”选项卡并向层次结构添加一个新对象。默认的对象类型是“Button”,但你也可以选择“自定义”。那个“自定义”项目实际上是一个空的GtkToolItem。然后,你可以使用常规的glade界面选择该空项目,并正常地向其添加小部件。这使我能够在几秒钟内将GtkEntry小部件添加到GtkToolbar中。 - monotasker