Icons and Menus

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
        # GNU General Public License for more details.
        # create the main window menus
        image create photo fileopen-16 -data {
        image create photo filesave-16 -data {
        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


"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, https://www.tcl-lang.org/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.