Mac style joined buttons (segmented control) with Gtk

為{幸葍}努か 提交于 2019-12-25 00:33:41

问题


I've seen it done in the ubuntu software center, and with a few gnome apps.

Where two buttons look like a single rectangle with a line through it, how is this being done?


回答1:


In GTK 3.0 and later, use the INLINE_TOOLBAR style class or LINKED style class.

#!/usr/bin/env python

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()
for button in buttons:
    toolbar.insert(button, -1)
style_context = toolbar.get_style_context()
style_context.add_class(Gtk.STYLE_CLASS_INLINE_TOOLBAR)

window = Gtk.Window()
window.set_size_request(200, 50)
window.add(toolbar)
window.connect('delete-event', Gtk.main_quit)
window.show_all()

Gtk.main()



回答2:


Fwiw, since I recently had the same question, here's another answer with an up-to-date citation and using C instead of some wrapper. (I use GTKmm, but I feel we should refer to the native C API unless a wrapper is explicitly stated.)

I wanted to achieve the same thing and eventually stumbled upon the answer, at the official page HowDoI: Buttons. This effect is achieved by putting the targeted Buttons in a Container and giving the latter the CSS class linked. (That page says using a Box is normal, but asBox will probably be deprecated soon and Grid is superior anyway... use a Grid.)

So:

GtkWidget *const grid = gtk_grid_new ();
GtkStyleContext *const context = gtk_widget_get_style_context (grid);
gtk_style_context_add_class (context, "linked");
/* Add your Buttons to the Grid */

That link also discusses some other handy embellishments we can make to GtkButtons, including the classes suggested-action and destructive-action.

For similar questions, it's very illustrative to browse the source of (A) an application that does what you want to replicate and (B) the Adwaita theme CSS.



来源:https://stackoverflow.com/questions/8277152/mac-style-joined-buttons-segmented-control-with-gtk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!