ruby 和 Gtk::FontChooserDialog.新字体大小

ruby and Gtk::FontChooserDialog.new font size

我用的是ruby2.6.3,编译源码安装。 使用 Gtk::FontChooserDialog.new 时,给定的默认字体大小为 10。 是否可以调用 Gtk::FontChooserDialog.new 不同的大小,例如 为 24,这样我就可以避免每次 select 字体时都必须更改大小。 这是我做事的方式:

dialog = Gtk::FontChooserDialog.new(:title => "Select font",
                                          :parent => self,                                         
                                          :action =>  Gtk::FileChooserAction::OPEN,
                                      :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])

我试过(在参数列表中):size => 24, :default_size => 24, 等等。这是行不通的。我只是在这里猜测。我搜索了很多,没有运气。我还在 test-gtk-font-chooser-dialog.rb 和其他文件中查看了 gem 示例目录,但没有运气。

我正在使用 Linux Mint Mate 19.1,几周前安装的。

您需要通过Pango.FontDescription设置尺寸。 Python 中的一个简短示例是:

font_chooser = Gtk.FontChooserDialog.new(title = "Select font", parent = self)
font_description = Pango.FontDescription.new()
font_description.set_size(24 * Pango.SCALE)
font_chooser.set_font_desc(font_description)

编辑 这是 Ruby 中的完整示例:

#!/usr/bin/ruby

'''
ZetCode Ruby GTK tutorial

This program runs a font dialog with a default (settable) font size.

Author: Jan Bodnar
Website: www.zetcode.com
Last modified: May 2014
'''

require 'gtk3'
require 'pango'

class RubyApp < Gtk::Window

    def initialize
        super
        init_ui
    end

    def init_ui

        set_title "Center"
        signal_connect "destroy" do 
            Gtk.main_quit 
        end

        button = Gtk::Button.new
        button.set_label "Font"    
        button.signal_connect "clicked" do 
            dialog = Gtk::FontChooserDialog.new(:title => "Select font", :parent => self, :action =>  Gtk::FileChooserAction::OPEN, :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])
            font_description = Pango::FontDescription.new
            font_description.set_size 24 * Pango::SCALE
            dialog.set_font_desc font_description
            dialog.run
        end

        add button
        set_default_size 300, 200
        set_window_position Gtk::WindowPosition::CENTER
        show_all
    end   
end


window = RubyApp.new
Gtk.main