Sunday, April 28, 2024
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 1510  / 1 Year ago, thu, december 22, 2022, 10:56:05

When you run the below code, it will show an inline toolbar in a window. Notice how the inline toolbar has a stand-out backbround. Is there a way to apply CSS to get rid of it and make blend with regular window color?





#!/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()


Using andrewsomething code, it's starting to look better but the "border" is still there and takes up space. Notice in the below screenshot the toolbar on the left is smaller than the regular buttons on the right:
ubiquity testing


More From » application-development

 Answers
7

Here we go:



example screenshot



First you need to find the default background color for the the current theme. Then you can inject it into the current css for the GtkToolbar class.



# 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)


Note that you need to from gi.repository import Gdk


[#35690] Saturday, December 24, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
itchlos

Total Points: 486
Total Questions: 115
Total Answers: 110

Location: Macau
Member since Fri, Apr 28, 2023
1 Year ago
;