# Tk: setting window class

**URL:** <https://www.ruby-forum.com/t/tk-setting-window-class/110904>\
**Category:** Ruby\
**Created:** [July 31, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/tk-setting-window-class/110904 "2007-07-31T23:09:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [July 31, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/tk-setting-window-class/110904/1 "2007-07-31T23:09:48Z")

</div>

I need to set (or at least have a sensible value of) X window class for  
all application’s windows, so that I can identify them for window  
manager (FVWM in this case, this is irrelevant though).

What can I do to achieve this?

Here’s what I’ve been able to learn so far:

The TkRoot window has class name derived from script file name (which is  
satisfactory for me) but TkToplevel instances have class name of  
TopLevel and resource IDs w00001, w00002 etc (I belive that using  
resource names would be possible in FVWM, so a way of setting this could  
solve my problem).

In documentation that I found:  
[http://www.jbrowse.com/text/rubytk\_en.html#label:18](http://www.jbrowse.com/text/rubytk_en.html#label:18)  
there is a definition:

TkToplevel.new(parent=nil, keys=nil)  
TkToplevel.new(parent=nil, screen=nil, classname=nil, keys=nil)

but calling TkToplevel.new(@parent, nil, ‘MyClass’) doesn’t set the  
window’s class.

## I apologize if this has been covered on the list before but searching for this topic is a bit difficult (I mean with keywords like “tktoplevel window class ruby”…).

Marcin S.

---

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [August 1, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/tk-setting-window-class/110904/2 "2007-08-01T20:51:39Z")

</div>

Hidetoshi NAGAI wrote:

> From: Marcin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> […]
> 
> > In documentation that I found:  
> > [http://www.jbrowse.com/text/rubytk\_en.html#label:18](http://www.jbrowse.com/text/rubytk_en.html#label:18)  
> > there is a definition:
> > 
> > TkToplevel.new(parent=nil, keys=nil)  
> > TkToplevel.new(parent=nil, screen=nil, classname=nil, keys=nil)
> > 
> > but calling TkToplevel.new(@parent, nil, ‘MyClass’) doesn’t set the  
> > window’s class.
> 
> I’m very sorry. That is a bug. The ‘classname’ argument doesn’t work.  
> Instead of that argument, please use one of the followings:
> 
> (1) t = TkToplevel.new(parent, :class=\>‘MyClass’)  
> or  
> t = TkToplevel.new(parent, :classname=\>‘MyClass’)

Thanks. This works.

> (2) class MyClass \< TkToplevel; end; t = MyClass.new(@parent)
> 
> If you want to use the widget name on the resource DB,  
> please use ‘widgetname’ option.  
> […]

## I only need to identify application’s windows in WM so that I can apply specific styles and functions to them. But I’ll keep this part about DB options in mind.

Marcin S.

---

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [August 1, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/tk-setting-window-class/110904/3 "2007-08-01T19:14:59Z")

</div>

From: Marcin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Tk: setting window class  
Date: Wed, 1 Aug 2007 06:09:47 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> In documentation that I found:  
> [http://www.jbrowse.com/text/rubytk\_en.html#label:18](http://www.jbrowse.com/text/rubytk_en.html#label:18)  
> there is a definition:
> 
> TkToplevel.new(parent=nil, keys=nil)  
> TkToplevel.new(parent=nil, screen=nil, classname=nil, keys=nil)
> 
> but calling TkToplevel.new(@parent, nil, ‘MyClass’) doesn’t set the  
> window’s class.

I’m very sorry. That is a bug. The ‘classname’ argument doesn’t work.  
Instead of that argument, please use one of the followings:

(1) t = TkToplevel.new(parent, :class=\>‘MyClass’)  
or  
t = TkToplevel.new(parent, :classname=\>‘MyClass’)

(2) class MyClass \< TkToplevel; end; t = MyClass.new(@parent)

If you want to use the widget name on the resource DB,  
please use ‘widgetname’ option.  
For example,

TkOptionDB.add(‘_mytop_background’, ‘yellow’)  
t = TkToplevel.new(parent, :widgetname=\>‘mytop’)

‘classname’ option and ‘widgetname’ option are available only on the  
Hash argument of TkToplevel.new or TkFrame.new method.  
Those cannot be changed after construction.  
It means that those option cannot be set in the block argument of  
‘new’ method.

Please see also an example of TkOptionDB (ext/tk/sample/tkoptdb.rb  
on the Ruby source tree).  
That includes how to give and treat procedures on the resource DB.
