By easier i mean - can i define a style or something and apply it to all labels in my program? I have a lot of labels in it and I don't want to type so much. I heard about "Pango Style" but can I apply it to all label widgets at once?
问题:
回答1:
The term: "lot of labels" is relative. Are we talking about 14 or 84? If it's closer to 84 you should probably be using Glade to create the interface then set the frames x-pad and y-pad properties. With CSS for gtk3 you'll have to pack any label in a frame for margin, padding or any of their variants (margin-top, padding-bottom) to work.
A GtkFrame is a child of GtkMisc. GtkMisc has the function gtk_misc_set_padding(). You can use that function on your labels without packing them into frames. But you'd have to set it for each label.
I don't know Python only C and some C++ but you could do something like this:
1) create a enum for all your labels starting with label0. (namedtuple in python I think) The reason I say to use an enum is that it's going to get very difficult later to keep track of all the labels without them.
2) create an array of pointers to GtkWidget, one for each label.
3) create your labels using the pointers to GtkWidget with the enum as the index. (here's where the enum is really needed)
4) create a for loop with gtk_misc_set_padding() in it. Use the GtkWidget array as the parameter in gtk_misc_set_padding(). Loop through each label, setting its padding.
I could provide a example but it would probably wouldn't be useful if you don't know C. If you'd still like it let me know.
回答2:
You could add_provider_for_screen.
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
def add_provider(widget):
screen = widget.get_screen()
style = widget.get_style_context()
provider = Gtk.CssProvider()
provider.load_from_data('label {\
border: 1px solid #000000;\
margin: 2px;\
}'.encode('utf-8'))
style.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
window = Gtk.Window()
label = Gtk.Label()
label2 = Gtk.Label()
box = Gtk.VBox()
box.add(label)
box.add(label2)
window.add(box)
label.set_label("asd1")
label2.set_label("asd2")
window.connect("realize", add_provider)
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
add_provider
needs to be run only once but after a widget is realized (doesn't matter which widget you will add to)