某些程序中的黑色标题小部件是什么?

在某些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() 

我得到一个这样的窗口: 在此处输入图像描述 它没有工具栏的暗色调。

编辑2:尽管j-johan-edwards的“具有特殊上下文的工具栏”在大多数程序中都是正确的,但在ubuntuone-control-panel中并非如此。 该程序有一个GtkVBox,可以包含任何范围的小部件(与工具栏不同)。 我仍然无法确定gtk-theme如何知道如何绘制窗口的那一部分。 在此处输入图像描述

但无论如何:现在一个工具栏对我来说已经足够了……

你是说这些吗?

GTK3工具栏

他们只是Gtk.Toolbar 。 像Banshee这样的应用程序不使用它们的原因是它们还没有移植到GTK + 3 ,并且获得了新的主题function,可以启用这样的工具栏。

要将您自己的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) 

将该上下文应用于问题示例会产生以下结果:

演示

关于问题的第二部分,即“如何将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() 

它看起来应该是这样的