xub*_*tix 22 python gtk application-development
在某些 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()
Run Code Online (Sandbox Code Playgroud)
我得到一个这样的窗口:
工具栏没有暗色调。
EDIT2:尽管 j-johan-edwards 的“带有特殊上下文的工具栏”答案在大多数程序中都是正确的,但在 ubuntuone 控制面板中却并非如此。这个程序有一个 GtkVBox,它可以包含任何范围的小部件(与工具栏不同)。我仍然无法确定 gtk-theme 如何知道如何绘制窗口的那部分。

但无论如何:现在一个工具栏对我来说就足够了......
Jje*_*jed 19
你是说这些吗?

他们只是Gtk.Toolbars。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)
Run Code Online (Sandbox Code Playgroud)
将该上下文应用于问题示例会导致:

关于您问题的第二部分,即“如何将 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)
...
Run Code Online (Sandbox Code Playgroud)
你可以通过创建一个辅助函数或扩展 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
Run Code Online (Sandbox Code Playgroud)
它只是检查您尝试插入的对象是否为 ToolItem,如果不是,则将其包裹在一个 ToolItem 中。用法示例:
主文件
#!/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()
Run Code Online (Sandbox Code Playgroud)
它应该是这样的
| 归档时间: |
|
| 查看次数: |
2716 次 |
| 最近记录: |