【问题标题】:Can I style GtkBox margin/padding with CSS only?我可以仅使用 CSS 设置 GtkBox 边距/填充样式吗?
【发布时间】:2016-02-23 15:41:09
【问题描述】:

如何使用 GTK 应用程序的 CSS 样式表进行这样的布局?

这里是示例代码:

#!/usr/bin/python

import gi

gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")

from gi.repository import Gtk, Gdk


# Main application window
# =======================
class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        self.connect("delete-event", Gtk.main_quit)

        self.set_name("main-window")

        # load style from file
        cssProvider = Gtk.CssProvider()
        cssProvider.load_from_path('style.css')

        # get the default screen for the default display
        screen = Gdk.Screen.get_default()

        # new object which will store styling information affecting widget
        styleContext = Gtk.StyleContext()
        styleContext.add_provider_for_screen(screen, cssProvider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

        self.resize(200, 200)

        # create box for another boxes
        self.box = Gtk.Box()
        self.box.set_name("box")
        self.add(self.box)

        self.box2 = Gtk.Box()
        self.box2.set_name("box2")
        self.box.pack_start(self.box2, False, False, 0)

        self.text = Gtk.Label.new()
        self.text.set_text('text')
        self.box2.pack_start(self.text, False, False, 0)

        self.box3 = Gtk.Box()
        self.box3.set_name("box3")
        self.box.pack_start(self.box3, False, False, 0)

        self.text2 = Gtk.Label.new()
        self.text2.set_text('text2')
        self.box3.pack_start(self.text2, False, False, 0)

# Create and show window
win = MainWindow()
win.show_all()
Gtk.main()

这是适用于 HTML 的 CSS 样式表

#box {
  background: blue;
  padding: 5px;
}

#box2 {
  background: red;
  margin: 5px;
}

#box3 {
  background: green;
  margin: 5px;
}

但结果是:

当然,我可以在 python 代码中添加填充/间距值,但仅适用于没有嵌套框的水平间隙。可以不用硬编码,只用 css 解决方案吗?

【问题讨论】:

    标签: python css margin padding gtk3


    【解决方案1】:

    现在不行。 GTK 不支持其小部件上的margin 属性,它仅支持绘制框架的小部件上的padding 属性。 (哪些元素绘制框架可能有点随意,但 Gtk.BoxGtk.Label 不这样做,这就是为什么您的示例不起作用的原因。您可以通过将它放在 Gtk.Frame 中来在任何小部件上伪造它.)

    This blog post 表明 marginpadding 计划在即将推出的 GTK 3.20 中的所有小部件上得到一致支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2018-08-03
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多