[Tutor] Creating menu shortcut key bindings

David bouncingcats at gmail.com
Fri Jun 2 05:56:51 EDT 2023


On Thu, 1 Jun 2023 at 21:25, Alan Gauld via Tutor <tutor at python.org> wrote:
> On 01/06/2023 12:35, David wrote:
>
> > Your event sequence does not specify any event type [2].
> >
> > Some event sequences allow the event type to be omitted
> > for brevity. For example [3]:
> >   'x' is the same as '<KeyPress-x>'
> >
> > However, I suspect that what you have might not be recognised.
> > Looking at working code I have here (without actually
> > testing what you have) I see that I have:
> >   '<Control-KeyPress-o>'
>
> I have code that binds <Control-space> and it works OK.
> I suspect the widget might be the issue. But its all speculation
> without knowing how the GUI builder constructs things.

Hi,

You are right. I tested binding
  'a'
  'KeyPress-a'
  'Control-a'
  'Control-KeyPress-a'
to button widgets and they all are responsive.

A small aside regarding the 'Shift' event modifier:
'Shift-Keypress-a' does not work.
'Keypress-A' is required for that.
I assume that is only a consideration for alphabetic
keys, because 'Shift-KeyPress-space' is responsive.

I understand that I am ignoring the 'pygubu' that Phil
is using, so my input to the conversation is probably
not of much use.

Even so, I have one more point that I noticed during testing,
that is worth stating explicitly on the chance that it might help Phil:

If event sequences are bound to a particular
widget 'w', then they will only be responsive when
widget 'w' has the keyboard focus, otherwise they
are completely ignored.

So when I see that Phil's code does:
  self.mainwindow.bind('<Control-o>', ...
it makes me wonder:
1) if self.mainwindow is the appropriate widget to bind to
2) if self.mainwindow has the keyboard focus at the time of the event

My final suggestion is to ask the tkinter mailing list:
  https://mail.python.org/mailman/listinfo/tkinter-discuss
It is low traffic, but responsive.


More information about the Tutor mailing list