Home > Enterprise >  How to set WM_CLASS on TkRoot in ruby
How to set WM_CLASS on TkRoot in ruby

Time:12-12

I'm trying to set the 'WM_CLASS' attribute for my ruby tk application. I've tried several ways, but I think it should work like that:

TkRoot.new(class: 'Test')

But that will err with:

<internal:kernel>:18:in `class': wrong number of arguments (given 1, expected 0) (ArgumentError)
    from /home/ben/.gem/ruby/3.0.0/gems/tk-0.4.0/lib/tk/root.rb:40:in `block in new'
    from /home/ben/.gem/ruby/3.0.0/gems/tk-0.4.0/lib/tk/root.rb:36:in `each'
    from /home/ben/.gem/ruby/3.0.0/gems/tk-0.4.0/lib/tk/root.rb:36:in `new'
    from examples/form.rb:19:in `initialize'
    from examples/form.rb:49:in `new'
    from examples/form.rb:49:in `<main>'

How to do it correctly?

CodePudding user response:

Tk (C api) is setting the the WM_CLASS values to the value of argv[0] which is the same as $0 in ruby. So to set WM_CLASS, one has to set $0 prior to requiring Tk. i.e.:

$0 = 'myWmClass'
require 'Tk'

# ... rest of the code follows here

This will set the application name (of WM_CLASS) to 'myWmClass' and the application class to 'MyWmClass' (so uppercase is forced here). If one wants to set the application name to something different, this can be done via Tk.appname('someFancyName') after requiring Tk. Note that even though Tk is required, the main window seems not to be created and so the newly created main window will have the correct appname right from the start. Also note, that using an uppercase string as application name may result in errors, as the official tk command reference states.

For any other toplevel window (besides the root window), WM_CLASS can be set on construction, e.i. TkToplevel.new(root, class: 'Toplevel'). Trying to do that on TkRoot will currently result in an exception. (It seems however to work in the python api.)

  • Related