in reply to Simple Tk question: colour of drop-down menu

Hi again,

Whew - I'm glad BlueBlazerRegular seconded my opinion - I thought I may have been out in left field on this one. I recalled a post I made a while ago (can't find it at the moment) where I produced some code that explored the children of various widgets and tried to recursively set options. This is probably not a good way to set options for widgets but may help you understand how some more complex constructs are created.

Here I have modified your code to recurse the children of the 'File' Menubutton widget. The only child it finds is a Tk::Menu object which it does manage to set the background color for (so the tear-off is purple now as well as the button).

Oddly enough the separator is still not purple unless you tear it off! Bizarre. My next experiment will be to bind that nexted foreach into a callback so that we can see how the children of Tk::Menu change as the menu is in different states.

#!/usr/bin/perl -w use strict; use Tk; my $main = MainWindow->new(); $main->minsize( qw(450 250) ); $main->title("Look! My first Perl/Tk window."); $main->configure(-background => 'Cyan' ); my $menu_bar = $main->Frame(-relief=>'groove', -borderwidth => 3, -background => 'purple', )->pack(-side =>'top', -fill=>'x'); my $file_mb = $menu_bar->Menubutton(-text=>'File', -background=>'purple', -activebackground=>'cyan', -foreground=>'white', )->pack(-side =>'left'); my $file_mb2 = $menu_bar->Menubutton(-text=>'Help', -background=>'purple', -activebackground=>'cyan', -foreground=>'white', )->pack(-side =>'left'); $file_mb->command(-label=>'Exit', -activebackground =>'magenta', -command=>sub{$main->destroy}); $file_mb->separator(); # Code to recurse the children of '$file_mb' # again - don't do this in real life. :) foreach ($file_mb->children) { print "$_\n"; $_->configure(-background => 'purple'); #Interestingly only Tk::Menu is found an it has no children... foreach ($_->children) { print "\t>$_\n"; if ($_->cget('-background')) { $_->configure(-background => 'purple'); } } } MainLoop();
{NULE}
--
http://www.nule.org