Gtk3按钮如何设置字体大小

3
我是一个使用Python和Gtk3编写小应用程序的开发人员。几乎一切都很顺利,但有一件事:我无法使用CSS更改按钮的字体大小。尽管我可以更改字体系列(比如Arial),颜色、背景颜色,但字体大小似乎被忽略了。你有什么建议吗?
以下是我的代码:
from gi.repository import Gtk, Gdk
class Monelio:
    def __init__(self):
        # load CSS
        style_provider = Gtk.CssProvider()
        css = open('monelio.css')
        css_data = css.read()
        css.close()
        style_provider.load_from_data(css_data)
        Gtk.StyleContext.add_provider_for_screen(
            Gdk.Screen.get_default(), 
            style_provider,     
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        )
        # load CSS
        style_provider = Gtk.CssProvider()
        css = open('monelio.css')
        css_data = css.read()
        css.close()
        style_provider.load_from_data(css_data)
        Gtk.StyleContext.add_provider_for_screen(
            Gdk.Screen.get_default(), 
            style_provider,     
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        )
        button1 = Gtk.Button()
        button1.set_label(" - ")
        button1.set_name("btn_moins_" + str(x+1))
        button1.set_size_request(80, 70)
        button1.get_style_context().add_class("btn_moins")
        button1.connect("clicked", self.on_btn_moins_clicked)
        button1.show()

并且我的 CSS 如下:
.btn_moins {
    background-color: orange;
    background: orange;
    color: white;
    font-weight: bold;
    font-size: 25;
    padding: 0 20px 0 20px; 
}

1
欢迎来到 Stack Overflow。您能提供一下您目前尝试过的代码示例吗?您可以点击问题标签下方的“编辑”按钮来进行操作。 - BlackVegetable
这是一个旧的帖子,但您可能想在CSS中为字体设置提供“单位”:font-size: 25px; 或 font-size: 25pt; 或 font-size: 2em; - RLI123
3个回答

2

使用CssProvider

在窗口创建时:

screen = Gdk.Screen.get_default()
gtk_provider = Gtk.CssProvider()
gtk_context = Gtk.StyleContext()
gtk_context.add_provider_for_screen(screen, gtk_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
#css = """#styled_button { font-weight: bold; font-size: 22; }""" #also valid
css = """#styled_button { font: bold 16}""" #bold is not necessary 
gtk_provider.load_from_data(css)

在按钮创建时:

button.set_name("styled_button")

但您不必设置小部件名称,您还可以...
  • Set style for all widgets: (css = "GtkButton { font: 16}")
  • Set style from methode:

    ...
    css = "GtkButton { font: 16}"
    ...
    
    def on_button_clicked(self, widget, valid):
        style_context = widget.get_style_context()
        if valid:
            style_context.add_class("bigger")
        else:
            style_context.remove_class("bigger")
    

2

我希望能够帮助你并学习如何在PyGobject中使用CSS,但是你只发布了代码的一小部分,这对我来说没有什么用处。

这里有一个字体的例子:

from gi.repository import Gtk

class LabelWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Label Example")

        hbox = Gtk.Box(spacing=10)
        hbox.set_homogeneous(False)
        vbox_left = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        vbox_left.set_homogeneous(False)
        vbox_right = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        vbox_right.set_homogeneous(False)

        hbox.pack_start(vbox_left, True, True, 0)
        hbox.pack_start(vbox_right, True, True, 0)

        label = Gtk.Label("This is a normal label")
        vbox_left.pack_start(label, True, True, 0)

        label = Gtk.Label()
        label.set_text("This is a left-justified label.\nWith multiple lines.")
        label.set_justify(Gtk.Justification.LEFT)
        vbox_left.pack_start(label, True, True, 0)

        label = Gtk.Label(
            "This is a right-justified label.\nWith multiple lines.")
        label.set_justify(Gtk.Justification.RIGHT)
        vbox_left.pack_start(label, True, True, 0)

        label = Gtk.Label("This is an example of a line-wrapped label.  It "
                          "should not be taking up the entire             "
                          "width allocated to it, but automatically "
                          "wraps the words to fit.\n"
                          "     It supports multiple paragraphs correctly, "
                          "and  correctly   adds "
                          "many          extra  spaces. ")
        label.set_line_wrap(True)
        vbox_right.pack_start(label, True, True, 0)

        label = Gtk.Label("This is an example of a line-wrapped, filled label. "
                          "It should be taking "
                          "up the entire              width allocated to it.  "
                          "Here is a sentence to prove "
                          "my point.  Here is another sentence. "
                          "Here comes the sun, do de do de do.\n"
                          "    This is a new paragraph.\n"
                          "    This is another newer, longer, better "
                          "paragraph.  It is coming to an end, "
                          "unfortunately.")
        label.set_line_wrap(True)
        label.set_justify(Gtk.Justification.FILL)
        vbox_right.pack_start(label, True, True, 0)

        label = Gtk.Label()
        label.set_markup("Text can be <small>small</small>, <big>big</big>, "
                         "<b>bold</b>, <i>italic</i> and even point to "
                         "somewhere in the <a href=\"http://www.gtk.org\" "
                         "title=\"Click to find out more\">internets</a>.")
        label.set_line_wrap(True)
        vbox_left.pack_start(label, True, True, 0)

        label = Gtk.Label.new_with_mnemonic(
            "_Press Alt + P to select button to the right")
        vbox_left.pack_start(label, True, True, 0)
        label.set_selectable(True)

        button = Gtk.Button(label="Click at your own risk")
        label.set_mnemonic_widget(button)
        vbox_right.pack_start(button, True, True, 0)

        self.add(hbox)

window = LabelWindow()        
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

但是CSS在哪里? - jcoppens

0

我尝试了@oxidworks的方法,虽然看起来应该可以工作,但我没有成功。

当我玩CSS时注意到的是,更改标签的字体大小也会影响按钮。因此,GtkButtons在内部使用GtkLabels。我将CSS属性设置在按钮的标签后代上,而不是在ID本身上(它不会抱怨),现在字体大小可以正常工作了。也许我是一个特例?

#some_button > label {
    font-size: 16px;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接