How to copy the GTK style of a widget and apply it to another?

人走茶凉 提交于 2019-12-11 03:59:21

问题


My current GTK popups look like this - note it takes the dark ambiance colour theme.

In GTK3.8 and later there are GTKMenuButtons - the popup looks like this - note it looks like it uses the button styling cues.

I like this style and I want my application popups to look the same so there is a better look - integration and feel.

I know I can override the background colour of the popup using this snippet of python code:

style = button.get_style_context()
color = style.get_background_color(Gtk.StateFlags.NORMAL)
popup_menu.override_background_color(Gtk.StateFlags.NORMAL, color)

It looks like this if I apply the button background colour.

I've no idea how to apply the button font colour to the popup.

More importantly there is that annoying black border - 1px wide?

Thus to my question - am I attempting this the correct way (overriding theme properties) or can I somehow apply the CSS styling of one widget (the button or the button popup) to the popup so I can mimic the menubutton popup styling?

More information - the GTKMenuButton source gtkmenubutton.c doesnt have any theming controls for the popup, thus I'm at a loss how the menubutton popup gets its theme.


回答1:


After further investigation I discovered that the style class of the widget (or container) affects the overall style of embedded objects.

Let me explain further with an example:

Construct a grid and attach the MenuButton containing the popup menu.

Adding the Toolbar StyleClass to the Grid influences all objects in that grid including the popup.

style = grid.get_style_context()
style.add_class(Gtk.STYLE_CLASS_TOOLBAR)

The result is this:


from gi.repository import Gtk

class MenuExampleWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Menu Example")

        self.set_default_size(200, 200)

        grid = Gtk.Grid()
        grid.insert_column(0)

        menu = Gtk.Menu()
        mitem1 = Gtk.MenuItem(label = "Item 1")
        mitem2 = Gtk.MenuItem(label = "Item 2")


        menub = Gtk.MenuButton(label='menu')
        menub.set_popup(menu)
        menub.set_align_widget(None)
        menub.show_all()

        menu.append(mitem1)
        menu.append(mitem2)
        menu.attach_to_widget(menub, None)
        menu.show_all()

        style = grid.get_style_context()
        style.add_class(Gtk.STYLE_CLASS_TOOLBAR)


        grid.attach(menub, 0,0,1,1)
        self.add(grid)

window = MenuExampleWindow()       
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()




回答2:


Assuming you use gtk3+

gtk_widget_get_style/gtk_widget_get_modifier_style and gtk_widget_set_style/gtk_widget_modify_style should do what you want. Be careful, there is a builtin precedence for which style gets applied, which you can not modify (see DocBook entries for the above functions)



来源:https://stackoverflow.com/questions/20506701/how-to-copy-the-gtk-style-of-a-widget-and-apply-it-to-another

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