Judismar Arpini Junior
Judismar Arpini Junior

Reputation: 431

Binding a keypress to a frame in tcl/tk

I looked everywhere for an answer, so I come here. The question is simple: why binding key pressing (e.g. Escape) doesn't work on frames? Example:

pack [frame .f]
bind .f <Escape> exit; #won't work

Upvotes: 0

Views: 1285

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137797

Frames can process key events just fine, but don't usually as they usually don't have the focus and won't take it by default when you click on them or use Tab to cycle around the things that are focusable.

The most direct fix is to just do:

focus .f

That makes keyboard events go to the frame now. However, you probably want to use this so that you can click-to-focus:

bind .f <1> {focus %W}

And this so that the widget participates in the tab-traversal:

.f configure -takefocus 1

Lastly, frames don't show anything when they have the focus since the configure their “highlight thickness” to have zero width. Let's change that:

.f configure -highlightthickness 2

That should be all you need (or at least it works for me when I test).

Upvotes: 1

Related Questions