I am working on a Tk application and ran into a problem with a "missing" <FocusOut> event. I have the main window with some buttons and menu and a frame with an grid of 9 x 9 text widgets. On the text widgets, I have bound the arrows to force the focus to move to different grid cell. So far, so good.

I also bind the <FocusOut> event from the text class to post process any changes that occurred in that text cell. Each cell needs to be processed as soon as the change is made (actually when you leave the cell). It works fine except if you change a text cell then press a button or menu selection, there is not a <FocusOut> event. Moving around with the arrows or using the mouse to change cells works fine.

I came up with a kludge fix, but I wonder if there might be a better way:

my $current_focus = 0; #widget that is currently in focus ... #main window bindings $main_window->bind( '<ButtonPress>'=> \&button_press); ... #text widget class bindings (9 x 9 = 81 text widgets) $text_widget->bind($text_class, '<FocusOut>'=> \&focus_out); $text_ +widget->bind($text_class, '<FocusIn>' => \&focus_in); ... #get focus on a cell - save the widget to be processed by button_press + later if a button is pressed sub focus_in { my($widget) = @_; $current_focus = $widget; #stash it away } #generate a fake focus_out on a cell when a button is pressed sub button_press { if ($current_focus ) { focus_out($current_focus); #fake a missing <FocusOut> event } } #lose focus on a cell - update master data and redraw cell/screen sub focus_out { my($widget) = @_; my $e = $widget->XEvent; # get event object # do stuff here... $current_focus = 0; #there is no current focus right now. <Foc +usIn> will create a new one... return 0; }

This works, but there could be problems with it long term if other event situations come up. Any other ideas?

Also is there any good documentation of the inner working of Tk, especially dealing with events? I have "mastering perl/Tk" and most of the O'Reilly books, but they don't go deep enough. Thanks. -Eugene


In reply to Dealing with missing TK FocusOut event by NatureFocus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.