How do I set the icon for a window using Ruby Tk?

I would like to be able to change the icon of a window displayed using
Ruby and Tk.

Using Perl and Tk I can do the following:

use strict;
use warnings;
use Tk;

my $mymainwindow = MainWindow->new();

my $myicon = $mymainwindow->Photo(-file=>"myimage.gif");

$mymainwindow->iconimage($myicon);
MainLoop();

How would I go about doing this using Ruby and Tk?

I have tried the following:

require 'tk'

mymainwindow = TkRoot.new()
myicon = TkPhotoImage.new {file "myimage.gif"}
mymainwindow.iconimage(myicon)

Tk.mainloop()

I get the error unknown option "-iconimage" at runtime.

What is the correct way to set the icon image for a window using Ruby
and Tk?

···

--
Posted via http://www.ruby-forum.com/.

Does anyone know if it is even possible to change the window icon using
Ruby and Tk?

···

--
Posted via http://www.ruby-forum.com/.

I'm replying only because nobody else seems willing to and not because I have the answer you're seeking. The short answer seems to be: no, but it might be platform dependent. The semi-official RubyTk documentation <http://www.jbrowse.com/text/rubytk_en.html&gt; that I consulted gives the following example:

<example>
Tk::Wm#iconbitmap(bitmap=None)
Sets the bitmap displayed in the icon to bitmap.

If nil is passed, the icon's bitmap setting is erased. If the argument is omitted, the current setting is returned.

require "tk"
Tk.root.iconify
p Tk.root.iconbitmap "question"
p Tk.root.iconbitmap
Tk.mainloop

=> ""
    "question"
</example>

Perhaps this code can change the window icon on platforms running X-Window/Motif, but I can't say from experience.

Running it on my platform, OS X with Aqua, produces no visible effect. But how could it? From OS X's point of view the Ruby program is a Unix command line session, and it displays the appropriate icon for such a session in the dock. If I minimize a Tk window, I get a bitmap thumbnail of the running window in the dock (again normal for OS X).

On OS X, I could get a custom icon by making the Ruby program a component of an application package (bundle) containing an icon resource, an info.plist file, and a launcher script written in AppleScript. On MS Windows, it's probably all very different. I wouldn't have a clue on how get a custom program icon there.

Regards, Morton

···

On Aug 21, 2006, at 9:18 PM, Dave Mihalik wrote:

Does anyone know if it is even possible to change the window icon using
Ruby and Tk?

Thanks for the help!

I am trying to change the icon using ruby and tk on Windows XP and
Windows Server 2003.

I tried:

require 'tk'

mymainwindow = TkRoot.new()
mymainwindow.iconify
mymainwindow.iconbitmap "question"
Tk.mainloop()

and also

require 'tk'

Tk.root.iconify
p Tk.root.iconbitmap "question"
Tk.mainloop()

Neither of these examples changes the icon.

···

--
Posted via http://www.ruby-forum.com/.

Dave Mihalik wrote in post #128770:

Thanks for the help!

I am trying to change the icon using ruby and tk on Windows XP and
Windows Server 2003.

I tried:

require 'tk'

mymainwindow = TkRoot.new()
mymainwindow.iconify
mymainwindow.iconbitmap "question"
Tk.mainloop()

and also

require 'tk'

Tk.root.iconify
p Tk.root.iconbitmap "question"
Tk.mainloop()

Neither of these examples changes the icon.

Sorry for the late answer, but:

root = TkRoot.new
myicon = TkPhotoImage.new('file' => 'myicon.gif')
root.iconphoto(myicon)

Only GIF, God knows, why.

Best regards:

Zoltan

···

--
Posted via http://www.ruby-forum.com/\.

Message-ID: <0dd86d61db27929a544f3190a6b3ef59@ruby-forum.com>

root = TkRoot.new
myicon = TkPhotoImage.new('file' => 'myicon.gif')
root.iconphoto(myicon)

Only GIF, God knows, why.

Standard Tcl/Tk has image handlers for PPM, PGM and GIF.
(Please see Tcl/Tk's "photo" comman manual.)
To support other image formats (e.g. JPEG), Img package is required.
When Img package is installed for your Tcl/Tk (which linked to Ruby/Tk),
you can use Img package by "require 'tkextlib/tkimg'".

···

From: Zoltán Hajdú <lists@ruby-forum.com>
Subject: Re: How do I set the icon for a window using Ruby Tk?
Date: Sat, 04 Jan 2014 20:27:16 +0100
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
Department of Artificial Intelligence, Kyushu Institute of Technology