当您运行以下代码时,它将在窗口中显示一个内联工具栏。请注意内联工具栏有一个突出的背景。有没有办法应用 CSS 来摆脱它并与常规窗口颜色融合?
#!/usr/bin/python3
from gi.repository import Gtk
button_names = [Gtk.STOCK_ABOUT, Gtk.STOCK_ADD, Gtk.STOCK_REMOVE, Gtk.STOCK_QUIT]
buttons = [Gtk.ToolButton.new_from_stock(name) for name in button_names]
toolbar = Gtk.Toolbar()
toolbar.set_show_arrow(False)
for button in buttons:
toolbar.insert(button, -1)
style_context = toolbar.get_style_context()
style_context.add_class(Gtk.STYLE_CLASS_INLINE_TOOLBAR)
grid = Gtk.Grid()
grid.add(toolbar)
label = Gtk.Label()
grid.add(label)
window = Gtk.Window()
window.set_size_request(200, 50)
window.add(grid)
window.connect('delete-event', Gtk.main_quit)
window.show_all()
Gtk.main()
使用 andrewsomething 代码,它开始看起来更好,但“边框”仍然存在并占用空间。请注意,在下面的屏幕截图中,左侧的工具栏比右侧的常规按钮小:
答案1
开始了:
首先你需要找到当前主题的默认背景颜色。然后你可以将它注入到 GtkToolbar 类的当前 css 中。
# Get the default window background color for the the current theme.
win_style_context = window.get_style_context()
bg = win_style_context.lookup_color('theme_bg_color')[1].to_string()
# Then we set that as the background for GtkToolbar
# We also make the boarder transparent
css_provider = Gtk.CssProvider()
toolbar_css = ".inline-toolbar.toolbar { background: %s; border-color: transparent; }" % (bg)
css_provider.load_from_data(toolbar_css.encode('UTF-8'))
screen = Gdk.Screen.get_default()
win_style_context.add_provider_for_screen(screen, css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
请注意,您需要from gi.repository import Gdk
答案2
您还可以使用:
css_provider = Gtk.CssProvider()
toolbar_css = ".inline-toolbar { background: alpha (@base_color, 0.0); border-color: transparent; }"
css_provider.load_from_data(toolbar_css.encode('UTF-8'))
win_style_context.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)