如何复制窗口小部件的GTK风格并将其应用于其他窗口?

编程入门 行业动态 更新时间:2024-10-24 08:29:30
本文介绍了如何复制窗口小部件的GTK风格并将其应用于其他窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前的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风格并将其应用于其他窗口?

本文发布于:2023-10-11 23:26:21,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口   应用于   部件   风格   并将其

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!