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

D

Dave Mihalik

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?
 
D

Dave Mihalik

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

Morton Goldberg

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

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> 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
 
D

Dave Mihalik

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top