如何使用CSS样式表为GTK应用程序制作这样的布局?
这是示例代码:
#!/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,0) self.text = Gtk.Label.new() self.text.set_text('text') self.Box2.pack_start(self.text,0) self.Box3 = Gtk.Box() self.Box3.set_name("Box3") self.Box.pack_start(self.Box3,0) self.text2 = Gtk.Label.new() self.text2.set_text('text2') self.Box3.pack_start(self.text2,0) # Create and show window win = MainWindow() win.show_all() Gtk.main()
这是CSS样式表,可以在HTML中使用
#Box { background: blue; padding: 5px; } #Box2 { background: red; margin: 5px; } #Box3 { background: green; margin: 5px; }
但结果是:
当然我可以在python代码中添加填充/间距值,但仅适用于没有嵌套框的水平间隙.可以在没有硬编码的情况下完成,只使用css解决方案吗?