【问题标题】:Ruby GTK3 how do I change the application theme?Ruby GTK3 如何更改应用程序主题?
【发布时间】:2020-03-07 06:22:26
【问题描述】:

我正在编写一个简单的应用程序来显示我的 GTK3 主题:

#!/usr/bin/ruby -W0
require 'gtk3'

app = Gtk::Application.new('org.xfce-colour.example', :flags_none)

app.signal_connect('activate') do |a|
    w = Gtk::ApplicationWindow.new(a)
    w.set_title('XFCE Colour')
    w.set_icon('/home/sourav/Documents/xfce_colour.png')
    w.set_default_size(70, 70)

    g = Gtk::Grid.new
    w.add(g)

    b1 = Gtk::Button.new(label: 'Button')
    g.attach(b1, 0, 0, 1, 1)

    b2 = Gtk::CheckButton.new('')
    g.attach(b2, 1, 0, 1, 1)

    b3 = Gtk::RadioButton.new('')
    g.attach(b3, 2, 0, 1, 1)

    b4 = Gtk::SearchEntry.new()
    g.attach(b4, 3, 0, 1, 1)

    b5 = Gtk::ToggleButton.new('Button')
    g.attach(b5, 0, 1, 1, 1)

    b6 = Gtk::ColorButton.new
    g.attach(b6, 1, 1, 1, 1)

    b7 = Gtk::Switch.new
    g.attach(b7, 2, 1, 1, 1)

    b8 = Gtk::Entry.new()
    g.attach(b8, 3, 1, 1, 1)

    [b1, b2, b3, b4, b5, b6, b7, b8].each do |b|
        begin
            b.margin = 6
        rescue Exception
            puts $!
        end
    end

    w.show_all
end

app.run

请在运行前注释w.set_icon('...') 行。


我想做的是从应用程序中更改 GTK 主题,就像在 A Widget Factory 中所做的那样。

例如,我可以将第一个参数发送给我的主题名称:

ruby layout.rb XFCE_Colour_Lite_Pink

并将主题设置为 XFCE_Colour_Lite_Pink:

我用的是XFCE4,如果能改一下WM主题就更好了。


编辑:here 所述,在 Python 中,我们有 Gtk.set_property() 方法。我为 Ruby 遵循了这个:

settings = Gtk::Settings.new
puts settings.set_property('XFCE_Colour_Lite_Yellow', 'Papirus')

我得到了错误:

Traceback (most recent call last):
    1: from layout.rb:7:in `<main>'
layout.rb:7:in `set_property': No such property: XFCE_Colour_Lite_Yellow (GLib::NoPropertyError)

【问题讨论】:

    标签: ruby gtk3


    【解决方案1】:

    在查找AWF的源代码后,我设法通过添加更改主题:

    Gtk::Settings.default.set_gtk_theme_name ARGV[0] if ARGV[0]
    

    如果 ARGV[0] 不是主题名称,它将使用默认主题。

    还有其他方法,例如:

    Gtk::Settings.default.gtk_application_prefer_dark_theme = true
    

    启用深色主题。我使用Gtk::Settings.default.methods.sort 对方法进行排序,因为当前的 ri 文档除了许可证信息之外没有涵盖任何内容。

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多