python - Gtk3按钮如何设置字体大小

标签 python button fonts size gtk3

我正在用 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; 
}

最佳答案

我想帮助你,我想学习如何在 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()

关于python - Gtk3按钮如何设置字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21974755/

相关文章:

java - Java Mac OSX native 外观和感觉是否尊重 UIManager 字体更改?

python - Sqlalchemy 提示外键不存在但实际上它存在

Android 2底部按钮并排

JavaScript。创建两个具有不同 url 的按钮。但最后制作的按钮会覆盖旧按钮

java - JFrame 文本显示错误

javascript - 有没有办法通过网络检查机器上安装了哪些字体?

python - 从askopenfilename打印变量

python - NumPy 中时间和空间高效的数组乘法

python - 在每个 x 位置合并 2 个列表

android - 执行与清除数据按钮相同的操作