Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

python - Can I style GtkBox margin/padding with CSS only?

How can i make layout like this using CSS stylesheet for GTK app?

box needle

Here is example code:

#!/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()

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;
}

But result is:

box result

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.)

This blog post reveals that margin and padding are planned to be supported consistently on all widgets in the upcoming GTK 3.20.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...