Tkinter international bind

Is there a way in Tkinter to bind a keyboard shortcut that will work across all keyboard layouts? (bind with scancode)

For example, I need a binding 'Control-Z'

that will work with the same physical key in the lower left corner of the keyboard in all layouts, for example:
    * Russian layout,
    * Greek layout, etc.

Here's what I've 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 looks like this:

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

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

Update-3

It just plays even without Russian keyboard or Russian Windows.

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

What is it. Now Alt-Shift will switch you to Russian, and you can enter the following funny characters:

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 within Russia.

All Windows applications (including wxPython) use Ctrl-

for undo, Ctrl-

cut, Ctrl-

copy, etc.

+3


source to share


4 answers


First of all, I am interested in Russian layout in Windows.

A quick and dirty workaround I'm currently using:

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.

This could be overcome if I could determine which layout is currently active, so second question: Tkinter defines the keyboard layout .

Another drawback is that due to the "generic" handling of modifier keys, it also fires when I press Ctrl-Alt-V, but that's a different story, just like for the English layout.

0


source


Another option, already suggested in the old 1999, is to switch from Tkinter to wxPython, where accelerator processing is done for all types of keyboard layouts automatically (Example example here: http://wiki.wxpython.org/AnotherTutorial ).



0


source


I have a partial and rather ugly solution for this. In the code below, I have a text widget window that has some kind of "built in" connection between the standard keyboard + events and handling them correctly. However, if I just changed the keyboard layout to, say, Russian, these functions no longer work. To solve the problem, I rewrote the implementation of these events and everything works fine now. But I feel a little upset about this decision. Does anyone have any better ideas? .. For example, is there a way to trigger (or emulate) a "normal" keypress in python tkinter? CtrlC

import tkinter

root = tkinter.Tk()

class MyWind (tkinter.Frame):
    def __init__(self, parent):
        tkinter.Frame.__init__(self, parent)
        self.create_UI()

    def create_UI(self):
        text_field = tkinter.Text(self)
        text_field.insert(tkinter.END, "Hello world")
        text_field.pack()

def print_event(event):
    print ("Event character code <char>: '%s'" % event.char)
    print ("   Event key symbol <keysym>: '%s'" % event.keysym)
    print ("   Event key code <keycode>: '%s'" % event.keycode)

def work_out_event(event): # Here is the solution
    widget_type = type(event.widget)
    if widget_type == tkinter.Text:
        content = event.widget.selection_get()
        print ("Selected content = '%s'" % content)
        root.clipboard_clear() 
        root.clipboard_append(content)

def lurker1(event):
    print ("Crtl + C (english) pressed!")       
    print_event(event)

def lurker2(event):
    print ("Crtl + C (russian) pressed!")
    print_event(event)
    work_out_event(event)        

root.bind("<Control-c>", lurker1)      # "C" character with the english keyboard layout
root.bind("<Control-ntilde>", lurker2) # "C" character with the russian keyboard layout

root.app = MyWind(root)
root.app.pack()
root.mainloop()

      

0


source


def copy(event):
    print 'Ctrl-C'
    master.clipboard_append('text')

      

and it works!

0


source







All Articles