问题描述
如何使用GTK应用程式的CSS样式表进行这样的布局?
How can i make layout like this using CSS stylesheet for GTK app?
这里是示例代码:
#!/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()
这里是CSS样式表HTML
Here is CSS stylesheet for this which will work in HTML
#box {
background: blue;
padding: 5px;
}
#box2 {
background: red;
margin: 5px;
}
#box3 {
background: green;
margin: 5px;
}
但结果是:
当然,我可以在python代码中添加填充/间距值,但只适用于没有嵌套框的水平间隙。可以不用硬编码,只使用css解决方案?
Of course I can add padding/spacing values in python code, but only for horizontal gaps without nesting boxes. Can it be done without hardcoding, with css-only solution?
推荐答案
不是现在。 GTK不支持其窗口小部件上的 margin
属性,它只支持绘制框架的窗口小部件的 padding
属性。 (哪些元素绘制框架可以有点任意,但 Gtk.Box
和 Gtk.Label
所以这就是为什么你的例子不工作,你可以通过把它放在 Gtk.Frame
里的任何小部件。)
Not right now. GTK doesn't support margin
properties on its widgets, and it only supports padding
properties on widgets that draw a frame. (Which elements draw a frame can be a bit arbitrary, but Gtk.Box
and Gtk.Label
don't, so that's why your example doesn't work. You can fake it on any widget by putting it inside a Gtk.Frame
though.)
显示计划在即将到来的GTK 3.20的所有小部件上始终支持 margin
和 padding
。
This blog post reveals that margin
and padding
are planned to be supported consistently on all widgets in the upcoming GTK 3.20.
这篇关于我可以用CSS样式GtkBox边距/填充吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!