Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mix buttons & menus

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I would like to add a button with an image at the top left of my app just before my menu bar but on the same line, is it possible and what is the best thing to do?

Thank!
 
Here is what I have done so far but it does not work..


frame .top -borderwidth 2 -relief sunken
pack .top -fill x
#
# LGC LOGO
#
set i [image create photo -file lgc_logo.gif]
set logo [checkbutton .top.but -image $i -selectcolor SeaGreen1]
pack .top.but -expand yes
#
#MENUS
#

menu .top.menuBar -tearoff 0
set f .top.menuBar.file
set h .top.menuBar.help
#File menu
.top.menuBar add cascade -menu $f -label "File" -underline 0
menu $f -tearoff 1
$f add command -label "Select SW project" -command {SelectPrj} -underline 0
#$m add separator
$f add command -label "Load from Pcf file.." -command {Loadpcf} -underline 0 -accelerator "Meta-l"
$f add command -label "Save to Pcf file.." -command {Savepcf} -underline 0 -accelerator "Meta-s"
$f add separator
$f add command -label "Exit" -underline 1 -command {exit} -accelerator "Meta-q"

#help menu
.top.menuBar add cascade -menu $h -label "Help" -underline 0
menu $h -tearoff 1
$h add command -label "About.." -command {About} -underline 0
$h add separator
$h add command -label "Help.." -command {Help} -underline 0

global f h

. configure -menu .menuBar
 
Well, a couple of things. First, it's best not to try to put simple buttons in the menubar. People have been trained to expect drop-down menus when they click in the menubar area of a window, and it's always best not to confuse users with non-standard interfaces unless you've got a really good reason.

Secondly, in theory, Tcl supports creating menus and menu items that have images or bitmaps instead of textual labels. All you need to do is specify a -image or -bitmap option instead of a -label option when you add your menu entry. For example:

Code:
set folderIcon [image create photo     -file D:/TclTest/folder.gif]
.mbar.file add command -image $folderIcon

(By the way, historically Tcl has not allowed a menu entry -- or button widget -- to display both a textual label and a bitmap or image simultaneously. This has changed with the introduction of the -compound option in Tcl 8.4. See for information.)

Okay, so in theory we could do the same thing to display an image in the menubar:

Code:
menu .mbar
. configure -menu .mbar

set folderIcon [image create photo     -file D:/TclTest/folder.gif]
.mbar add cascade -image $folderIcon     -menu .mbar.special
menu .mbar.special -tearoff 0

This code actually works on Unix systems. But it doesn't work on Windows systems. The reason is that Tk hooks into the Windows native mechanisms for menubars, and it doesn't include the support for placing an image in a Windows-native menubar. I actually discussed the problem before in thread287-287624. And it turns out that we can use the same workaround that I came up with in that thread.

What we're going to do is go back to the old way of creating menubars in Tcl: specifically, you create several menubutton widgets, and pack them into a frame at the top of your window. This approach is generally deprecated in favor of the newer -menu option for toplevel windows, but it's going to be your only option in this situation if you're working on Windows.

Here's a quick example of how you'd build your menubar with menubuttons and include an image in the menubar (note that you'll have to supply your own image file for this to work):

Code:
# First create a frame for the menubar
# and pack it at the top of the window.

frame .mbar
pack .mbar -side top -anchor w

# Now let's create some placeholder contents
# for the window.

pack [text .t] -expand yes -fill both

# Create our image object. Here's where you'll
# have to supply your own image file.

set folderIcon [image create photo     -file D:/TclTest/folder.gif]

# Create a menubutton to put in the menubar,
# and have it display the image object we
# just created.

# By the way, I find that giving the menubuttons
# an internal -pady of 2 is more visually
# pleasing and closer to the appearance of
# the standard menubars.

menubutton .mbar.folder -image $folderIcon     -menu .mbar.folder.menu -pady 2

# Hook it up to a menu with a couple of
# dummy entries.

menu .mbar.folder.menu -tearoff 0

.mbar.folder.menu add command     -label "Open" -command {puts "Open"}
.mbar.folder.menu add command     -label "Close" -command {puts "Close"}

# Create another menu just to show what it
# will look like.

menubutton .mbar.view -text "View"     -underline 0 -pady 2     -menu .mbar.view.menu

menu .mbar.view.menu

.mbar.view.menu add command     -label "Zoom In" -underline 5     -command {puts "Zoom In"}
.mbar.view.menu add command     -label "Zoom Out" -underline 5     -command {puts "Zoom Out"}

# Pack the menubuttons into the "menubar"
# frame.

pack .mbar.folder .mbar.view -side left

# The Alt key accelerators don't seem to
# work by default on menubuttons. So, let's
# create our own procedure to post a
# menubutton's popup menu properly.

proc postMenu {menu} {
    set x [winfo rootx $menu]
    set y [expr {[winfo height $menu]            + [winfo rooty $menu]} ]
    $menu.menu post $x $y
}

# Now actually create the bindings to post
# the menus.

bind . <Alt-KeyPress-f> {
    postMenu .mbar.folder
}
bind . <Alt-KeyPress-v> {
    postMenu .mbar.view
}
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thanks a lot Avia. Since I am using Unix only the first solution worked fine!

Another question: is it possible to add an image to a window's title bar?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top