Search code examples
pythontkinterkeyboard-shortcutscopy-pastekeyboard-events

Python tkinter (copy/paste not working with other languages)


I found out that whenever i switch the language from english to russian tkinter stops reacting to Ctrl+C, Ctrl+V or Ctrl+X.

It still works when i switch back to english, even if the text is in russian.

I tried all code snippets i could find on stack exchange remotely related to copy-paste topic, adding stuff similar to original code like self.bind('<Control-м>', self.paste) ("м" is the same button in russian as "v" in english), but still nothing works.

Would really appreciate any help/ideas on how to fix it.


Solution

  • from Tkinter import Tk, Entry   
    
    def _onKeyRelease(event):
        ctrl  = (event.state & 0x4) != 0
        if event.keycode==88 and  ctrl and event.keysym.lower() != "x": 
            event.widget.event_generate("<<Cut>>")
    
        if event.keycode==86 and  ctrl and event.keysym.lower() != "v": 
            event.widget.event_generate("<<Paste>>")
    
        if event.keycode==67 and  ctrl and event.keysym.lower() != "c":
            event.widget.event_generate("<<Copy>>")
    
    
    master = Tk()
    master.geometry("500x500+1+1")
    master.bind_all("<Key>", _onKeyRelease, "+")
    Entry(master).pack()
    Entry(master).pack()
    Entry(master).pack()
    master.mainloop()