Updated 2013-06-18 22:27:12 by escargo

ZipGuy 2013-Jun 17: - You can find out my email address by clicking on Zipguy.

I just managed to find the right code to add an Icon to a menu. It was hard to find this option, in the many, many commands within Tk. Here's an example of how to get it to look, like this:

How did I do this?

It's real easy, once you know the subcommand of Tk menus. The subcommand is "-compound left" combined with an "-image imagename" that you've already defined. When coded, it should look like this...
        # menuicon.tcl
        # ---------------
        # Written to show how you can use Icons with Menus, by Zipguy.
        # ---------------
        # This program is free software; you can redistribute it and/or
        # modify it under the terms of the GNU General Public License
        # as published by the Free Software Foundation; either version 2
        # of the License, or (at your option) any later version.
        #
        # This program is distributed in the hope that it will be useful,
        # but WITHOUT ANY WARRANTY; without even the implied warranty of
        # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        # GNU General Public License for more details.
        #
        # create the main window menus
        
        image create photo fileopen-16 -data {
           R0lGODlhEAAQAIMAAPwCBASCBMyaBPzynPz6nJxmBPzunPz2nPz+nPzSBPzqnPzmnPzinPzenAAAAAAAACH5BAEAAAAALAAAAAAQABAAAARTEMhJq724hp1n8MDXeaJgYtsnDANhvkJRCcZxEEiOJDIlKLWDbtebCBaGGmwZEzCQKxxCSgQ4Gb/BbciTCBpOoFbX9X6fChYhUZYU3vB4cXTxRwAAIf5oQ3JlYXRlZCBieSBCTVBUb0dJRiBQcm8gdmVyc2lvbiAyLjUNCqkgRGV2ZWxDb3IgMTk5NywxOTk4LiBBbGwgcmlnaHRzIHJlc2VydmVkLg0KaHR0cDovL3d3dy5kZXZlbGNvci5jb20AOw==
        }
        image create photo filesave-16 -data {
           R0lGODlhEAAQAIQAAPwCBFRShGRmzPT6/Oz2/OTy/Nzu/NTm/AQCBMTi/Lze/Mzm/KzW/NTq/LTa/AQCxOTi5Nze3Nza3MzOzLy+xNTS1MTGxMzKzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAQABAAAAVkICCOZCkGaKqqpxAMRFEYRx0gQfvGRZ0oKZ2MdlgogC6doVc8MgJJADRQaxidU13t8HMwnlGoa4UShM2PtFp9fkAikomcQnm0IebKxGKp3/MTF3R2OVICboB7foVSZGQmkCR+IQAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7
        }
        wm title . "Menu with Icon"
        wm minsize . 300 0
        menu .menu -tearoff 0
        # add the "file" menu
        set m .menu.file
        menu $m -tearoff 0
        .menu add cascade -label "File" -menu $m -underline 0
        $m add command -label "New" -command "# some command"  -underline 0 
        $m add command -label "Open" -command "# some command" -underline 0 \
                -image                 fileopen-16   -compound left
        $m add command -label "Save" -command "# some command" -underline 0 \
                -accelerator Ctrl+S\
                -image                 filesave-16   -compound left
        $m add command -label "Exit" -underline 1 -command "exit;WriteIni;exit"
        . configure -menu .menu

Explanation

"image create photo fileopen-16 -data {R0lGODlh...}" defines the image,
"-image fileopen-16 -compound left" combines the image with the menu item.

Do you get it now? If you don't believe it (and IF you have tcl installed), you can download the code above, save it as menuicon.tcl, run it, and it should look like this:

You can look at the code and get to understand what it does.

What is that code that makes a photo?

You will need to understand how base64 works if you have the source (for you photo) in your source code, like I do here.

From base64: "Base64 (RFC 3548) is a way to encode arbitrary binary data in such a way that the result contains only printable characters and are thus able to pass unchanged even through old applications which are not 8bit-ASCII clean.".

The bad news, is that 8 bits turn into 10 bits, which means that it's slightly larger! The good news, is that it will download as a txt file, and run just fine.

What is the Image command?

You can see the image command in your help files, if you've installed Tcl/Tk, or you can look at it at image, http://www.tcl.tk/man/tcl8.4/TkCmd/image.htm, or you can do a search on 'image' on the left of this screen.

Here is an extemely interesting program that does Creating image photo data from GIF files. I have a modified version of it, in progress so far, which I plan to improve further.