我目前的GTK弹出窗口看起来像这样 - 注意它需要黑暗的氛围色彩主题。
在GTK3.8及更高版本中,有 GTKMenuButtons - 弹出窗口看起来像这样 - 注意它看起来像使用了按钮样式提示。
我喜欢这种风格,我希望我的应用程序弹出窗口看起来是一样的,所以有一个更好的外观 - 集成和感觉。
我知道我可以用这段代码覆盖弹出窗口的背景颜色:
style = button.get_style_context() color = style.get_background_color(Gtk.StateFlags.NORMAL) popup_menu。 override_background_color(Gtk.StateFlags.NORMAL,color)如果我应用按钮背景colou r。
我不知道如何将按钮字体颜色应用到弹出窗口。
更重要的是那个令人讨厌的黑色边框 - 1px宽?
因此,对于我的问题 - 我尝试这是正确的方式(覆盖主题属性),或者我可以以某种方式将一个小部件(按钮或按钮弹出窗口)的CSS样式应用到弹出窗口,以便我可以模仿menubutton弹出式样式?
更多信息 - GTKMenuButton源代码 gtkmenubutton.c 没有任何主题的弹出式控件,因此我不知道menubutton弹出窗口如何获得它的主题。
>进一步调查后,我发现widget(或容器)的样式类会影响嵌入对象的整体风格。让我进一步用一个例子来解释: p>
构建一个网格并附加包含弹出式菜单的MenuButton。
添加工具栏样式网格类将影响该网格中的所有对象,包括弹出窗口。
style = grid.get_style_context() style.add_class(Gtk.STYLE_CLASS_TOOLBAR)结果如下:
from gi.repository import Gtk class MenuExampleWindow(Gtk.Window): $ b $ 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()
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.
解决方案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()
更多推荐
如何复制窗口小部件的GTK风格并将其应用于其他窗口?
发布评论