Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

python - Tkinter international bind

Is there a way in Tkinter to bind a combination of keys that will work in all keyboard layouts? (bind by scancode)

For example, I need 'Control-Z' binding that will work with the same physical key in the lower left corner of the keyboard in all layouts, such as:
    * Russian layout,
    * Greek layout, etc.

Here's what I tried:

from Tkinter import *
root=Tk()
def f(event):
    print 'undo'
button1=Button(root, text=u'Button')
button1.pack()
button1.bind('<Control-z>', f)
root.mainloop()

It doesn't work for Russian and Greek keyboard layouts.

Update-2:

I did some more experiments with Windows and now the general rule is like that:

    1) If the language is based on latin character set, keys are mapped "by value" (German, French, Dvorak) so that the same action is mapped to different physical keys.
    2) If it is not (eg Russian, Greek), then all major accelerators are mapped "by position" (to match the corresponding English letter usually marked on the same key).

Only the second case needs special attention. Any ideas if this is implemented in some lib already?

Update-3

It is simply reproduced even without Russian keyboard or Russian Windows.

1) Start->Control Panel->Regional and Language Options
2) Language->Details
3) Add Russian language.

That's it. Now Alt-Shift will switch you to Russian and you'll be able to type the following funny symbols:

Russian keyboard

another Alt-Shift will switch you back.

Forget what Wikipedia says about phonetic Russian layouts. They are not used these days. At least inside Russia.

All Windows applications (including wxPython ones) use Ctrl-я for undo, Ctrl-ч for cut, Ctrl-с for copy and so on.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I'm primarily interested in is Russian layout in Windows.

The quick and dirty workaround I currently use is:

import Tkinter

def copy(event):
    print 'Ctrl-C'

root = Tkinter.Tk()
root.bind('<Control-ntilde>', copy)
root.mainloop()

which could potentially lead to a conflict with <Ctrl + actual ntilde> in some other language.

It could be overcome if I could determine which layout is currently active, thus second question: Tkinter determine keyboard layout.

Another drawback is that due to 'universal' treatment of modifier keys it also fires when I press Ctrl-Alt-V, but that's another story as it applies to English layout as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...